Ansible

Ansible notify when changed

In Ansible, a handler refers to a particular task that executes when triggered by the notify module. Handlers perform an action defined in the task when a change occurs in the remote host.

Handlers are helpful when you need to perform a task that relies on a specific task’s success or failure. For example, you can set a handler to send Apache logs if the service goes down.

This article will help you understand how to define and use the Ansible handler in playbooks.

Basic Usage

To illustrate how to use the Ansible handler, let us a simple example. The playbook below will install the Apache http server and use a handler to start the service.

---
- hosts
: all
  become
: true
gather_facts
: no
  tasks
:
  - name
: "install apache"
    package
:
      name
: apache2
      state
: present
    notify
:
startapache
  handlers
:
    - name
: startapache
      service
:
        name
: apache2
        state
: started

In the example playbook above, we start by installing the apache2 server using the package module. We then use a notify module to set a notify action.

The final step is to configure a handler to run after the server is installed. The name of the notification should be the same as the name used in the handler module. Otherwise, the specified handler will fail.

Save and run the above playbook:

ansible-playbookhandlers.yml

How to Configure Ansible Notify Handler For Alert Message

You can also configure an ansible handler to display a message instead of taking actions such as restarting services.

For example, the playbook below restarts the apache service and displays a message to the user.

---
- hosts
: all
  become
: true
  tasks
:
  - name
: "restart apache"
    service
:
      name
: apache2
      state
: restarted
    notify
:
apacherestarted
  handlers
:
    - name
: apacherestarted
      debug
:
        msg
: "The Apache service restarted successfully"

In the example above, we register a simple handler that shows a successful message when the apache service is restarted.

Below is an example output:

TASK [restart apache]
**********************
changed
: [192.168.0.111]

RUNNING HANDLER [apache restarted]
************************************
ok
: [192.168.0.111] => {
    "msg"
: "The Apache service restarted successfully."
}

How to Configure Ansible Notify Handler For Multiple Handlers

Suppose you want to update the system and restart two services after the update. As shown in the playbook below, you can define two handlers to perform as actions:

---
- hosts
: all
  become
: true
  tasks
:
  - name
: "update the system"
    apt
:
update_cache
: yes
      upgrade
: dist
    notify
:
     -apache
      -mysql
  handlers
:
    - name
: apache
      service
:
        name
: apache2
        state
: restarted
    - name
: mysql
      service
:
        name
: mysqld
        state
: restarted

In this example playbook, we update the system using the apt module. Next, we use the notify modules to define two tasks.

Using handlers, we define actions for each notify task. In our example, we restarted both Apache and MySQL services.

Conclusion

This tutorial has shown you how to define and use the Ansible notify and handlers module to perform an action where a task is complete.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list