Saturday, July 15, 2017

Handlers and Notify

What is Notify


Notify is something used to notify the handlers. In simple words I have done some changes now to which handlers task i need to inform about service restart

A simple handler notify example

---
- hosts: ss
  tasks:
   - name: Install ftp
     yum: name=vsftpd state=present
     notify: echo
  handlers:
   - name: echo
     debug: msg="This test message will be printed"

...

After first run, we will get:

TASK [Install ftp] *******************changed: [192.***.***.**]

RUNNING HANDLER [do an echo] *****************ok: [192.***.***.**] => {
    "msg": "This will be printed"
}

PLAY RECAP *********************************************************************
192.***.***.**             : ok=1    changed=1    unreachable=0    failed=0  
As the state is changed ( Look at the changed=1 part ), debug message will be printed.

After second run, we will get:

TASK [Install ftp] ************************************************************
ok: [192.***.***.**]

PLAY RECAP *********************************************************************
192.***.***.**             : ok=0    changed=0    unreachable=0    failed=0  

The thing to look at is handler isn't called unlike first run, because state isn't changed in the second run ( because vsftpd is already installed ).

Like this we can have multiple configuration changes for ssh and each task with notify module Even after multiple notification from multiple notify command. Handler will Suppose if we wan't to restart nfs service when the exports file is updated. If this two tasks are mentioned in task then irrespective of a exports file modification nfs service will restart. So we will notify and handlers to handle this situation.

The notify handler will be executed if and only if state is changed as a result of the task.
Notify sets a trigger to run the specified handler based on the status of the task.

What is Handler

Handlers are lists of tasks that will run only when it is notified by "notify" module. These names of handlers are unique. It will work like task only but the only difference is task will run on own but handlers will execute when it gets a notification. Below example will make you clear.

Suppose if we wan't to restart nfs service when the exports file is updated. If this two tasks are mentioned in task then irrespective of a exports file modification nfs service will restart. So we will notify and handlers to handle this situation.

Whenever the tasks update the file we will mention the nfs restart handler in the notify section.

---
- hosts: ss
  tasks:
  - name: Update exports file
    script: "/home/bhr_moham607/ymls/SERVER_INFO_OUTPUT_U.sh"
    register: serverinfo
    notify: Restart NFS

  handlers:
  - name: Restart NFS
    service: name=nfs state=restart

---
- hosts: ss
  tasks:
  - name: Ensure docker repo is added
    script: "/home/bhr_moham607/ymls/SERVER_INFO_OUTPUT_U.sh"
    register: serverinfo
    notify: Done serverinfo
    when: ansible_local.serverinfo | d(0) == 0
  handlers:
  - name: Done serverinfo
    copy: content= '{{ serverinfo }}' dest= /home/bhr_moham607/ymls/test_file_2505

No comments:

Post a Comment