ansiblehandlers

Get Ansible ongoing notify list


I'm looking for a way to get the list (possibly an ansible system variable) that would contain current handler notifications triggered by a playbook.

In other words, at some point in a role, I would like to add a "when" condition to a task that would test if a "restart notification" has not been sent yet -> do this task when restart is not in the notification list

Any idea ?

Thanks a lot!


Solution

  • I finally came to a solution, quite acceptable, using facts.

    I use a handler to set a fact and another one to perform the restart. I associate both of them with the listen parameter to the same notification.

    Besides, a reload handler performs the reload only when that fact is not defined or set to false.

    - name: Register restart Logstash
      set_fact:
        restart_notified: true
      listen: "Restart Logstash"
    
    - name: Restart Logstash service
      systemd:
        name: logstash
        daemon_reload: true
        state: restarted
      listen: "Restart Logstash"
    
    - name: Reload Logstash
      systemd:
        name: logstash
        daemon_reload: true
        state: reloaded
      when: restart_notified is not defined or not restart_notified
    

    I then use where needed, either Restart Logstash or Reload Logstash, and only one is performed, restart or reload, but never both.