diff --git a/examples/roles/var_naming_pattern/tasks/include_task_with_vars.yml b/examples/roles/var_naming_pattern/tasks/include_task_with_vars.yml index a8437a356e..5151cd3142 100644 --- a/examples/roles/var_naming_pattern/tasks/include_task_with_vars.yml +++ b/examples/roles/var_naming_pattern/tasks/include_task_with_vars.yml @@ -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. diff --git a/src/ansiblelint/rules/var_naming.md b/src/ansiblelint/rules/var_naming.md index 88db2d8b05..3386a0cbc2 100644 --- a/src/ansiblelint/rules/var_naming.md +++ b/src/ansiblelint/rules/var_naming.md @@ -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: diff --git a/src/ansiblelint/rules/var_naming.py b/src/ansiblelint/rules/var_naming.py index 6660ba741a..02870c5008 100644 --- a/src/ansiblelint/rules/var_naming.py +++ b/src/ansiblelint/rules/var_naming.py @@ -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, @@ -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: @@ -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("__"),