Skip to content

Commit 1a9c031

Browse files
ssbarneacognifloyd
andauthored
Add ability to use use sub-rule matches on skip or warn lists (#2251)
* Add ability to use use sub-rule matches on skip or warn lists You can now add entries like `yaml[document-start]` to the skip_list or warn_list, instead of adding the entire rule, allowing you to have more granular filtering. Closes: #2248 Closes: #1794 * Update src/ansiblelint/rules/yaml.md Co-authored-by: Jacob Floyd <[email protected]> * Update .ansible-lint Co-authored-by: Jacob Floyd <[email protected]> * Update src/ansiblelint/app.py Co-authored-by: Jacob Floyd <[email protected]> Co-authored-by: Jacob Floyd <[email protected]>
1 parent 0b744d8 commit 1a9c031

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

.ansible-lint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ warn_list:
6060
- git-latest
6161
- experimental # experimental is included in the implicit list
6262
# - role-name
63+
# - yaml[document-start] # you can also use sub-rule matches
6364

6465
# Some rules can transform files to fix (or make it easier to fix) identified
6566
# errors. `ansible-lint --write` will reformat YAML files and run these transforms.

src/ansiblelint/app.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ def count_results(self, matches: List[MatchError]) -> SummarizedResults:
104104
fixed_failures = 0
105105
fixed_warnings = 0
106106
for match in matches:
107-
if {match.rule.id, *match.rule.tags}.isdisjoint(self.options.warn_list):
107+
# tag can include a sub-rule id: `yaml[document-start]`
108+
# rule.id is the generic rule id: `yaml`
109+
# *rule.tags is the list of the rule's tags (categories): `style`
110+
if {match.tag, match.rule.id, *match.rule.tags}.isdisjoint(
111+
self.options.warn_list
112+
):
108113
if match.fixed:
109114
fixed_failures += 1
110115
else:
@@ -129,7 +134,10 @@ def _get_matched_skippable_rules(
129134
) -> "Dict[str, BaseRule]":
130135
"""Extract the list of matched rules, if skippable, from the list of matches."""
131136
matches_unignored = [match for match in matches if not match.ignored]
132-
matched_rules = {match.rule.id: match.rule for match in matches_unignored}
137+
# match.tag is more specialized than match.rule.id
138+
matched_rules = {
139+
match.tag or match.rule.id: match.rule for match in matches_unignored
140+
}
133141
# remove unskippable rules from the list
134142
for rule_id in list(matched_rules.keys()):
135143
if "unskippable" in matched_rules[rule_id].tags:

src/ansiblelint/rules/yaml.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ ansible-lint, not from yamllint.
77

88
You can fully disable all yamllint violations by adding `yaml` to the `skip_list`.
99

10-
Specific tag identifiers that are printed at the end of rule name,
11-
like `yaml[trailing-spaces]` or `yaml[indentation]` can also be be skipped, allowing
12-
you to have a more fine control.
10+
Specific tag identifiers that are printed at the end of the rule name,
11+
like `yaml[trailing-spaces]` or `yaml[indentation]` can also be skipped, allowing
12+
you to have more control.
13+
14+
Keep in mind that `ansible-lint` does not take into consideration the warning level
15+
of yamllint; we treat all yamllint matches as errors. So, if you want to treat
16+
some of these as warnings, add them to `warn_list`.
1317

1418
### Problematic code
1519

0 commit comments

Comments
 (0)