-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor no-loop-var-prefix rule (#2470)
* docs lint rule no-loop-var * chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor no-loop-var-prefix - rename rule to loop-var-prefix and keep alias for old name - make rule produce more detailed messages - add documentation to the rule Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sorin Sbarnea <[email protected]>
- Loading branch information
1 parent
01d2e6d
commit e4de1cd
Showing
7 changed files
with
90 additions
and
38 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# loop-var-prefix | ||
|
||
This rule avoids conflicts with nested looping tasks by configuring a variable prefix with `loop_var`. | ||
Ansible sets `item` as the loop variable. | ||
You can use `loop_var` to specify a prefix for loop variables and ensure they are unique to each task. | ||
|
||
This rule can produce the following messages: | ||
|
||
- `[loop-var-prefix[missing]` - Replace unsafe implicit `item` loop variable by adding `loop_var: <loop_var_prefix>...`. | ||
- `[loop-var-prefix[wrong]` - Loop variable should start with <loop_var_prefix> | ||
|
||
This is an opt-in rule. | ||
You must enable it in your Ansible-lint configuration as follows: | ||
|
||
```yaml | ||
enable_list: | ||
- loop-var-prefix | ||
``` | ||
## Problematic Code | ||
```yaml | ||
--- | ||
- name: Example playbook | ||
hosts: localhost | ||
tasks: | ||
- name: Does not set a prefix for loop variables. | ||
ansible.builtin.debug: | ||
var: item | ||
loop: | ||
- foo | ||
- bar # <- These items do not have a unique prefix. | ||
- name: Sets | ||
ansible.builtin.debug: | ||
var: zz_item | ||
loop: | ||
- foo | ||
- bar | ||
loop_control: | ||
loop_var: zz_item # <- This prefix is not unique. | ||
``` | ||
## Correct Code | ||
```yaml | ||
--- | ||
- name: Example playbook | ||
hosts: localhost | ||
tasks: | ||
- name: Sets a unique prefix for loop variables. | ||
ansible.builtin.debug: | ||
var: zz_item | ||
loop: | ||
- foo | ||
- bar | ||
loop_control: | ||
loop_var: my_prefix # <- Specifies a unique prefix for loop variables. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters