Skip to content

Commit

Permalink
Avoid no-handlers false positives (#3702)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Sep 4, 2023
1 parent 9387687 commit 798746c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
11 changes: 11 additions & 0 deletions examples/playbooks/no_handler_pass.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@
ansible.builtin.debug:
msg: why isn't this a handler
when: result | changed

handlers:
# If this task would have being under 'tasks:' it should have triggered
# the rule, but under 'handlers:' it should not.
- name: Reproduce bug 3646
loop: "{{ _something_done.results }}"
loop_control:
label: "{{ item.item.name }}"
when: item.changed
ansible.builtin.debug:
msg: "{{ item.item.name }} changed"
14 changes: 7 additions & 7 deletions src/ansiblelint/rules/no_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ def matchtask(
task: Task,
file: Lintable | None = None,
) -> bool | str:
if task["__ansible_action_type__"] != "task":
if task["__ansible_action_type__"] != "task" or task.is_handler():
return False

when = task.get("when")
result = False

if isinstance(when, list):
if len(when) > 1:
return False
return _changed_in_when(when[0])
if isinstance(when, str):
return _changed_in_when(when)
return False
if len(when) <= 1:
result = _changed_in_when(when[0])
elif isinstance(when, str):
result = _changed_in_when(when)
return result


if "pytest" in sys.modules:
Expand Down
4 changes: 4 additions & 0 deletions src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,10 @@ def skip_tags(self) -> list[str]:
skip_tags: list[str] = self.raw_task.get(SKIPPED_RULES_KEY, [])
return skip_tags

def is_handler(self) -> bool:
"""Return true for tasks that are handlers."""
return ".handlers[" in self.position

def __repr__(self) -> str:
"""Return a string representation of the task."""
return f"Task('{self.name}' [{self.position}])"
Expand Down

0 comments on commit 798746c

Please sign in to comment.