FreeKB - Ansible Handlers Notify Plugin
Ansible - Handlers Notify Plugin

The handlers plugin allow you to do something once after multiple tasks are run. For example, let's say you have two (or more) tasks that use the file module to create a file. The notify module could be used to associate a unique string, such as files created, with the tasks. In this scenario, you would want the notify module to be identical in each task.

tasks:
  - name: create foo.txt
    file:
      path: /tmp/foo.txt
      state: touch

    notify: files created

  - name: create bar.txt
    file:
      path: /tmp/bar.txt
      state: touch

    notify: files created

 

With only the above, running the playbook would return something like this. When using the notify module, handlers must also be in the playbook.

TASK [create foo.txt] 
ERROR! The requested handler 'files created' was not found in either the main handlers list nor in the listening handlers list

 

After tasks have, you could then use handlers to do something. In this example, handlers is simply used to print "files have been created". Notice that tasks and handlers are at the same level, meaning that handlers is not a task.

Notice in handlers that the name parameter is "files created". The name parameter must be an exact match of the string that was used in the notify parameter.

tasks:
  - name: create foo.txt
    file:
      path: /tmp/foo.txt
      state: touch

    notify: files created

  - name: create bar.txt
    file:
      path: /tmp/bar.txt
      state: touch

    notify: files created

handlers:
  - name: files created
    debug:
      msg: "files have been created"

 

Now something like this should be returned.

TASK [create foo.txt] 
changed: [server1.example.com]

TASK [create bar.txt] 
changed: [server1.example.com]

RUNNING HANDLER [files created] 
ok: [server1.example.com] => {
    "msg": "files have been created"
}

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 1cdd1 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |