Skip to content

Commit 6b82bba

Browse files
committed
feat: detect missing prefix in role's handlers vars (#4816)
1 parent 43e758b commit 6b82bba

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

.config/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ switchport
9191
sysvinit
9292
taskshandlers
9393
testinfra
94+
thefoo
9495
timesyncd
9596
tmpfs
9697
tomlsort
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
- name: Echo thefoo
3+
ansible.builtin.command: echo thefoo
4+
changed_when: true
5+
register: thefoo
6+
7+
- name: Echo thebar
8+
ansible.builtin.command: echo thebar
9+
changed_when: true
10+
register: role_vars_prefix_detection_thebar
11+
12+
- name: Test ansible.builtin.set_fact without role prefix
13+
ansible.builtin.set_fact:
14+
foo: bar
15+
16+
- name: Test set_fact with role prefix
17+
ansible.builtin.set_fact:
18+
role_vars_prefix_detection_foo: bar

src/ansiblelint/rules/var_naming.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ def matchtask(
250250
results = []
251251
task_prefix = Prefix()
252252
role_prefix = Prefix()
253-
if file and file.parent and file.parent.kind == "role":
254-
role_prefix = Prefix(file.parent.path.name)
253+
if file and file.role:
254+
role_prefix = Prefix(file.role)
255255
ansible_module = task["action"]["__ansible_module__"]
256256
# If the task uses the 'vars' section to set variables
257257
# only check role prefix for include_role and import_role tasks 'vars'
@@ -426,7 +426,7 @@ def test_var_naming_with_role_prefix(
426426
Lintable("examples/roles/role_vars_prefix_detection"),
427427
rules=default_rules_collection,
428428
).run()
429-
assert len(results) == 2
429+
assert len(results) == 4
430430
for result in results:
431431
assert result.tag == "var-naming[no-role-prefix]"
432432

0 commit comments

Comments
 (0)