Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for included role prefix instead of current one #3473

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
ansible.builtin.include_tasks: ../tasks/included-task-with-vars.yml
vars:
var_naming_pattern_foo: bar

- name: include_task_with_vars | Foo
ansible.builtin.include_role:
name: bobbins
vars:
bobbins_foo: bar
# ^ this is valid because for include/import, the prefix should be of the
# included role and from of the current role.
7 changes: 7 additions & 0 deletions src/ansiblelint/rules/var_naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ Possible errors messages:
- `var-naming[no-reserved]`: Variables names must not be Ansible reserved names.
- `var-naming[read-only]`: This special variable is read-only.

!!! note

When using `include_role` or `import_role` with `vars`, vars should start
with included role name prefix. As this role might not be compliant
with this rule yet, you might need to temporarily disable this rule using
a `# noqa: var-naming[no-role-prefix]` comment.

## Settings

This rule behavior can be changed by altering the below settings:
Expand Down
8 changes: 6 additions & 2 deletions src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:

return results

def matchtask(
def matchtask( # noqa: C901
self,
task: Task,
file: Lintable | None = None,
Expand All @@ -211,8 +211,13 @@ def matchtask(
filename = "" if file is None else str(file.path)
if file and file.parent and file.parent.kind == "role":
prefix = file.parent.path.name
ansible_module = task["action"]["__ansible_module__"]
# If the task uses the 'vars' section to set variables
our_vars = task.get("vars", {})
if ansible_module in ("include_role", "import_role"):
action = task["action"]
if isinstance(action, dict):
prefix = action.get("name", "").split("/")[-1]
for key in our_vars:
match_error = self.get_var_naming_matcherror(key, prefix=prefix)
if match_error:
Expand All @@ -222,7 +227,6 @@ def matchtask(
results.append(match_error)

# If the task uses the 'set_fact' module
ansible_module = task["action"]["__ansible_module__"]
if ansible_module == "set_fact":
for key in filter(
lambda x: isinstance(x, str) and not x.startswith("__"),
Expand Down