Failed to parse the YAML fileANS-E9001Package installs should not use `latest`ANS-E4003`become_user` requires `become` to work as expectedANS-E5001Shells that use pipes should set the `pipefail` optionANS-E3006Use `command` instead of `shell`ANS-E3005Commands should not change things if nothing needs to be doneANS-E3001Should not use `command` instead of arguments to modulesANS-E3002Environment variables don't work as part of commandANS-E3004Use `module` instead of `command`ANS-E3003Relative path is not needed in roleANS-E4004Mercurial checkouts should have explicit revisionANS-E4002Git checkouts must contain explicit versionANS-E4001Found key duplicationANS-E9002Replace `local_action` with `delegate_to: localhost`ANS-E5004All tasks should be named uniquelyANS-E5002Referenced files must existANS-E5005Tasks that run when changed should likely be handlersANS-E5003
Tasks that run when changed should likely be handlersANS-E5003
If a task has a when: result.changed
setting, it is effectively acting as a handler.
Sometimes you want a task to run only when a change is made on a machine. For example, you may want to restart a service if a task updates the configuration of that service, but not if the configuration is unchanged. Ansible uses handlers to address this use case. Handlers are tasks that only run when notified. Each handler should have a globally unique name.
Bad practice
- name: Template configuration file
ansible.builtin.template:
src: template.j2
dest: /etc/foo.conf
register: copyTemplate
- name: Restart memcached
ansible.builtin.service:
name: memcached
state: restarted
when: copyTemplate.changed
- name: Restart apache
ansible.builtin.service:
name: apache
state: restarted
when: copyTemplate.changed
Recommended
- name: Template configuration file
ansible.builtin.template:
src: template.j2
dest: /etc/foo.conf
notify:
- Restart memcached
- Restart apache
handlers:
- name: Restart memcached
ansible.builtin.service:
name: memcached
state: restarted
- name: Restart apache
ansible.builtin.service:
name: apache
state: restarted