chore(ansible): handlers gotchas

This commit is contained in:
Michele Cereda
2024-08-12 21:23:38 +02:00
parent 8a2bf02b2c
commit 5f5eb891d9
3 changed files with 46 additions and 1 deletions

View File

@@ -13,6 +13,7 @@
1. [Error handling](#error-handling)
1. [Using blocks](#using-blocks)
1. [Output formatting](#output-formatting)
1. [Handlers](#handlers)
1. [Roles](#roles)
1. [Get roles](#get-roles)
1. [Assign roles](#assign-roles)
@@ -504,6 +505,46 @@ $ ANSIBLE_STDOUT_CALLBACK='json' ansible-playbook --inventory='localhost,' 'loca
}
```
## Handlers
Blocks and `import_tasks` tend to make the handlers unreachable.
Instead of using blocks, give the same listen string to all involved handlers:
```diff
- - name: Block name
- block:
- - name: First task
- …
- - name: N-th task
- …
+ - name: First task
+ listen: Block name
+ …
+ - name: N-th task
+ listen: Block name
+ …
```
Instead of using `ìmport_tasks`, use `include_tasks`:
```diff
- name: First task
- import_tasks: tasks.yml
+ include_tasks: tasks.yml
```
Handlers **can** notify other handlers:
```yaml
- name: Configure Nginx
ansible.builtin.copy: …
notify: Restart Nginx
- name: Restart Nginx
ansible.builtin.copy: …
```
## Roles
### Get roles