How to Work With Ansible Handlers?

0
96
Spread the love
  • 28
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    137
    Shares

Ansible
Ansible Handlers

This post talks about Ansible Handler,How to use Ansible Handlers?,ansible handlers notify,ansible handlers vs tasks,ansible handlers debug,ansible handlers global,ansible handlers example,ansible handler multiple tasks,ansible handler arguments,ansible add handler.

Handlers

Instead of executing several tasks,we want some tasks(like-restart the servers)to be executed only when some event or status change occur.

---
   -hosts:db
    user:ansadmin
    become:true
    become_methods:sudo
     tasks:
       -name:Install httpd package
         yum:
           name:"httpd"
           state:"installed"
        -name:starting the httpd service
         service:
           name:"httpd"
           state:"started"
        -name:Copying the index file
          copy:
            src:"/etc/ansible/index.html"
            dest:"/var/www/html/index.html"
         -name:Restarting the server
           service:
             name:"httpd"
             state:"restarted"
…

The above playlist always restarts the server irrespective of the fact that any change happens or not.
To avoid this we can use handlers.

handlers:
     -name:Restarting the server
        services:
          name:"httpd"
          state:"restarted"

In task we need to include –

Notify:
- Restarting the server

Note:

The handler name and the task level notify name should be same.Here notify name and task name should match and the comparison is case sensitive.

In this scenario,if a change in the task happens then handler will run.

 

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 28
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    137
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here