Skip to content

Commit

Permalink
Docs: no handler rule doc update (#2446)
Browse files Browse the repository at this point in the history
* no handler rule doc update

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
  • Loading branch information
3 people committed Sep 19, 2022
1 parent e8a2eb9 commit f132d11
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/ansiblelint/rules/no_handler.md
@@ -1,10 +1,12 @@
# no-handler

If a task has a `when: result.changed` condition, it is effectively acting as a
[handler](https://docs.ansible.com/ansible/latest/user_guide/playbooks_handlers.html).
You could use `notify` and move that task to `handlers`.
This rule checks for the correct handling of changes to results or conditions.

If a task has a `when: result.changed` condition, it effectively acts as a
[handler](https://docs.ansible.com/ansible/latest/user_guide/playbooks_handlers.html).
The recommended approach is to use `notify` and move tasks to `handlers`.
If necessary you can silence the rule by add a `# noqa: no-handler` comment at the end of the line.

## Problematic Code

```yaml
Expand All @@ -17,20 +19,22 @@ This rule checks for the correct handling of changes to results or conditions.
dest: "/tmp/placeholder"
content: "Ansible made this!"
mode: 0600
register: result # <-- we register the result of the task

register: result # <-- Registers the result of the task.
- name: Second command to run
ansible.builtin.debug:
msg: The placeholder file was modified!
when: result.changed # <-- this triggers no-handler rule
when: result.changed # <-- Triggers the no-handler rule.
```

```yaml
---
# Optionally silences the rule.
when: result.changed # noqa: no-handler
```

## Correct Code

Below you can see the corrected code, which has the same functionality while
avoiding recording a `result` variable. For simple cases, this approach is to
be preferred but you can always silence the rule by adding a
`# noqa: no-handler` comment at the end of the line.
The following code includes the same functionality as the problematic code without recording a `result` variable.

```yaml
---
Expand All @@ -43,7 +47,7 @@ be preferred but you can always silence the rule by adding a
content: "Ansible made this!"
mode: 0600
notify:
- Second command to run # <-- handler will run only when file is changed
- Second command to run # <-- Handler runs only when the file changes.
handlers:
- name: Second command to run
ansible.builtin.debug:
Expand Down

0 comments on commit f132d11

Please sign in to comment.