Skip to content

Commit

Permalink
Fix for var-naming rule to not trigger on builtin variables and not b…
Browse files Browse the repository at this point in the history
…reak on include_tasks
  • Loading branch information
audgirka committed May 17, 2023
1 parent 8ef5ca4 commit ed2c720
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ def get_var_naming_matcherror(
)
return None

def filter_builtins_dbl_underscores(
self,
vars_to_filter: dict[str, Any],
) -> filter[str]:
"""Return filtered dictionary of variables."""
return filter(
lambda x: isinstance(x, str)
and not x.startswith("__")
and not x.startswith("ansible_"),
vars_to_filter,
)

def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
"""Return matches found for a specific playbook."""
results: list[MatchError] = []
Expand All @@ -95,7 +107,7 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
return results
# If the Play uses the 'vars' section to set variables
our_vars = data.get("vars", {})
for key in our_vars:
for key in self.filter_builtins_dbl_underscores(our_vars):
match_error = self.get_var_naming_matcherror(key)
if match_error:
match_error.filename = str(file.path)
Expand Down Expand Up @@ -131,7 +143,7 @@ def matchtask(
prefix = file.parent.path.name
# If the task uses the 'vars' section to set variables
our_vars = task.get("vars", {})
for key in our_vars:
for key in self.filter_builtins_dbl_underscores(our_vars):
match_error = self.get_var_naming_matcherror(key, prefix=prefix)
if match_error:
match_error.filename = filename
Expand All @@ -143,10 +155,7 @@ def matchtask(
# breakpoint()
ansible_module = task["action"]["__ansible_module__"]
if ansible_module == "set_fact":
for key in filter(
lambda x: isinstance(x, str) and not x.startswith("__"),
task["action"].keys(),
):
for key in self.filter_builtins_dbl_underscores(task["action"].keys()):
match_error = self.get_var_naming_matcherror(key, prefix=prefix)
if match_error:
match_error.filename = filename
Expand Down

0 comments on commit ed2c720

Please sign in to comment.