Skip to content

Commit a84c5d9

Browse files
authored
Do not trigger no_handler upon complex conditions (#3437)
1 parent 76adb30 commit a84c5d9

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

examples/playbooks/no_handler_fail.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,3 @@
2525
ansible.builtin.debug:
2626
msg: why isn't this a handler
2727
when: result['changed'] == true
28-
29-
- name: This should be a handler 5 # noqa: literal-compare
30-
ansible.builtin.debug:
31-
msg: why isn't this a handler
32-
when:
33-
- result['changed'] == true
34-
- another_condition
35-
36-
- name: This should be a handler 6
37-
ansible.builtin.debug:
38-
msg: why isn't this a handler
39-
when:
40-
- first_condition
41-
- result.changed

examples/playbooks/no_handler_pass.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
- conditionA
3636
- conditionB
3737

38+
- name: Check when with a list of size 1
39+
ansible.builtin.debug:
40+
var: result
41+
when:
42+
- conditionA
43+
3844
- name: Registering task 1
3945
ansible.builtin.command: echo Hello
4046
register: r1
@@ -49,6 +55,16 @@
4955
ansible.builtin.command: echo Hello
5056
when: r1.changed and r2.changed
5157

58+
- name: Use when with or # noqa: no-changed-when
59+
ansible.builtin.command: echo Hello
60+
when: r1.changed or conditionA
61+
62+
- name: Use when with list of conditions # noqa: no-changed-when
63+
ansible.builtin.command: echo Hello
64+
when:
65+
- r1.changed
66+
- conditionA
67+
5268
- name: Registering task
5369
ansible.builtin.command: echo Hello
5470
register: r

src/ansiblelint/rules/no_handler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _changed_in_when(item: str) -> bool:
3636
return False
3737
item_list = item.split()
3838

39-
if {"and", "not"} & set(item_list):
39+
if {"and", "or", "not"} & set(item_list):
4040
return False
4141
return any(
4242
changed in item
@@ -75,9 +75,9 @@ def matchtask(
7575
when = task.get("when")
7676

7777
if isinstance(when, list):
78-
for item in when:
79-
if _changed_in_when(item):
80-
return True
78+
if len(when) > 1:
79+
return False
80+
return _changed_in_when(when[0])
8181
if isinstance(when, str):
8282
return _changed_in_when(when)
8383
return False
@@ -92,7 +92,7 @@ def matchtask(
9292
@pytest.mark.parametrize(
9393
("test_file", "failures"),
9494
(
95-
pytest.param("examples/playbooks/no_handler_fail.yml", 7, id="fail"),
95+
pytest.param("examples/playbooks/no_handler_fail.yml", 5, id="fail"),
9696
pytest.param("examples/playbooks/no_handler_pass.yml", 0, id="pass"),
9797
),
9898
)

0 commit comments

Comments
 (0)