How to notify a remote service using Ansible

ansibleansible-playbook

In order to let checks appear in Uchiwa when a new sensu-client is added, the sensu-server and sensu-api need to be restarted. At the moment there are 5 clients and one server. Everytime a new sensu-client is added using ansible the checks do not appear in Uchiwa. At the moment, I need to login to the sensu-server and restart the services. How to automate this using ansible?

According to this documentation there are handlers:

handlers:
    - name: restart memcached
      service: name=memcached state=restarted
      listen: "restart web services"
    - name: restart apache
      service: name=apache state=restarted
      listen: "restart web services" 

that could be called using notify:

tasks:
    - name: restart everything
      command: echo "this task will restart the web services"
      notify: "restart web services"

so that the service will be restarted if a change occurs in for example the config, but how to notify a remote service, e.g. notify service on IP-B from IP-A?

Best Answer

(listen is a 2.2 new feature which is not released as of writing, so I won't use it)

A notify is just like a regular tasks (or so) except it is triggered by an event, so basically you could do something like that, this is kind of ugly (and insecure as the machine should have ssh access to the remote servers) but should work.

handlers:
    - name: restart my remote service
      command: ssh user@myserver -- service restart myservice

But Ansible has a more elegant manner to handle this with delegate_to

handlers:
    - name: restart my remote service
      service: name=myserver state=restarted
      delegate_to: myserver

What it does, it the handler task will be executed on the server(s) you've have delegate the task. and then plug-in to your task as you told in the question.