Skip to content

Commit

Permalink
Do not trigger no_handler upon complex conditions (#3437)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaus-tux committed May 15, 2023
1 parent 76adb30 commit a84c5d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
14 changes: 0 additions & 14 deletions examples/playbooks/no_handler_fail.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,3 @@
ansible.builtin.debug:
msg: why isn't this a handler
when: result['changed'] == true

- name: This should be a handler 5 # noqa: literal-compare
ansible.builtin.debug:
msg: why isn't this a handler
when:
- result['changed'] == true
- another_condition

- name: This should be a handler 6
ansible.builtin.debug:
msg: why isn't this a handler
when:
- first_condition
- result.changed
16 changes: 16 additions & 0 deletions examples/playbooks/no_handler_pass.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
- conditionA
- conditionB

- name: Check when with a list of size 1
ansible.builtin.debug:
var: result
when:
- conditionA

- name: Registering task 1
ansible.builtin.command: echo Hello
register: r1
Expand All @@ -49,6 +55,16 @@
ansible.builtin.command: echo Hello
when: r1.changed and r2.changed

- name: Use when with or # noqa: no-changed-when
ansible.builtin.command: echo Hello
when: r1.changed or conditionA

- name: Use when with list of conditions # noqa: no-changed-when
ansible.builtin.command: echo Hello
when:
- r1.changed
- conditionA

- name: Registering task
ansible.builtin.command: echo Hello
register: r
Expand Down
10 changes: 5 additions & 5 deletions src/ansiblelint/rules/no_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _changed_in_when(item: str) -> bool:
return False
item_list = item.split()

if {"and", "not"} & set(item_list):
if {"and", "or", "not"} & set(item_list):
return False
return any(
changed in item
Expand Down Expand Up @@ -75,9 +75,9 @@ def matchtask(
when = task.get("when")

if isinstance(when, list):
for item in when:
if _changed_in_when(item):
return True
if len(when) > 1:
return False
return _changed_in_when(when[0])
if isinstance(when, str):
return _changed_in_when(when)
return False
Expand All @@ -92,7 +92,7 @@ def matchtask(
@pytest.mark.parametrize(
("test_file", "failures"),
(
pytest.param("examples/playbooks/no_handler_fail.yml", 7, id="fail"),
pytest.param("examples/playbooks/no_handler_fail.yml", 5, id="fail"),
pytest.param("examples/playbooks/no_handler_pass.yml", 0, id="pass"),
),
)
Expand Down

0 comments on commit a84c5d9

Please sign in to comment.