Ansible – Enabling Handlers – Success handlers

Ansible_Logo

At times when certain tasks are executed by a playbook, it may be desired to run certain other task immediately. However, if the first task did not get executed successfully for what ever reason, we may not want to trigger the other task to get executed. Below are two use cases as examples to explain the need for Handlers:

  • In a web server already running and displaying contents, it may be desired to restart or reload the web server when ever the “httpd.conf” file is changed.
  • In a web server already running and displaying contents, it may be desired to restart the server when ever the “index.html” file is updated.

The below playbook is designed to copy an “index” file to managed node, when ever an httpd service is “started” by the playbook. Here the file will not get copied if the service is “already” running.

Below is the content in the “index file”.

[root@centos9vm ~]# cat shiju.html

==== ====
This is Shiju

======= ===

Let us write a playbook to perform the above mentioned tasks.

[root@centos9vm ~]# cat handlers.yml

===== ====
– – –
– name: Copy a file to managed node if the httpd server that is stopped in present state is started by the play book
    hosts: 192.168.48.129
    tasks:
        – name: Restart httpd server
            ansible.builtin.service:
                name: httpd
                state: started
                enabled: false
            notify: Copy Index file

    handlers:
        – name: Copy Index file
            ansible.builtin.copy:
                src: shiju.html
                dest: /var/www/html/index.html

===== ===

Let us ensure the web service is already running and observer the web page that is getting displayed.

[root@centos9vm ~]# curl http://192.168.48.129

======== ==
The initial ONE

====== ====

Now let us run the playbook. Since the httpd service is already running, the new file should not get copied.

[root@centos9vm ~]# ansible-navigator run -m stdout handlers.yml

===== === =

PLAY [Copy a file to managed node if the httpd server that is stopped in present state is started by the play book] ***

TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]

TASK [Restart httpd server] ****************************************************
ok: [192.168.48.129]

PLAY RECAP *********************************************************************
192.168.48.129 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

==== ====

Let us check the web page that is getting displayed.

[root@centos9vm ~]# curl http://192.168.48.129

======== ==
The initial ONE

====== ====

Let us stop the web service running on the managed node using ansible Ad Hoc command

[root@centos9vm ~]# ansible 192.168.48.129 -m command -a “systemctl stop httpd”

===== ===
192.168.48.129 | CHANGED | rc=0 >>

===== ===

Let us run the playbook again

[root@centos9vm ~]# ansible-navigator run -m stdout handlers.yml

===== ===

PLAY [Copy a file to managed node if the httpd server that is stopped in present state is started by the play book] ***

TASK [Gathering Facts] *********************************************************
ok: [192.168.48.129]

TASK [Restart httpd server] ****************************************************
changed: [192.168.48.129]

RUNNING HANDLER [Copy Index file] **********************************************
changed: [192.168.48.129]

PLAY RECAP *********************************************************************
192.168.48.129 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

==== ===

Notice the above output and the section that confirms the index file got copied. Let us verify the web page again.

[root@centos9vm ~]# curl http://192.168.48.129

===== ===
This is Shiju

===== ===