Ansible Handlers

Overview

Ansible Handlers are tasks that are only performed when a calling task has successfully changed something.

Updating Docker

Say for example you want to try and update docker. There isn’t always an update available but if there is an update and the server is updated, docker needs to be restarted.

In the roles/docker/tasks directory, the main.yaml file looks like:

---
- name: update docker
  yum:
    name: docker
    state: latest
  notify:
  - Restart docker

In the roles/docker/handlers directory, the main.yaml file looks like:

---
- name: Restart docker
  systemd:
    daemon_reload: yes
    name: docker
    state: restarted

Notice in the first code block the notify line followed by the Handler to call, Restart docker. Note that only one task with that name can exist in the namespace and if called, only the last named Handler will be called and only one time.

If docker can be updated, the following example shows the results. Note the changed at the start of the lines indicating an upgrade or restart has occurred. The ok indicates no change was performed.

PLAY [kube-bldr0-0-worker] ***********************************************************************************************************************************
 
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [bldr0cuomknode1]
ok: [bldr0cuomknode3]
ok: [bldr0cuomknode2]
 
TASK [docker : update docker] ***************************************************************************************************************************
changed: [bldr0cuomknode1]
changed: [bldr0cuomknode2]
changed: [bldr0cuomknode3]
 
RUNNING HANDLER [docker : Restart docker] *********************************************************************************************************************
changed: [bldr0cuomknode1]
changed: [bldr0cuomknode2]
changed: [bldr0cuomknode3]
 
NO MORE HOSTS LEFT *******************************************************************************************************************************************
 
PLAY RECAP ***************************************************************************************************************************************************
bldr0cuomknode1            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
bldr0cuomknode2            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
bldr0cuomknode3            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

If you run the playbook again, the Handler will not be called as an update isn’t necessary. Again, note the ok at the start of the lines indicating no change occurred.

PLAY [kube-bldr0-0-worker] ***********************************************************************************************************************************
 
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [bldr0cuomknode1]
ok: [bldr0cuomknode2]
ok: [bldr0cuomknode3]
 
TASK [docker : update docker] ***************************************************************************************************************************
ok: [bldr0cuomknode1]
ok: [bldr0cuomknode2]
ok: [bldr0cuomknode3]
 
PLAY RECAP ***************************************************************************************************************************************************
bldr0cuomknode1            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
bldr0cuomknode2            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
bldr0cuomknode3            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

References

This entry was posted in ansible, Computers and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *