Mikhail T.
Mikhail T.

Reputation: 3987

Can I notify a handler on another host?

An application I'm configuring involves a slave service running on multiple machines, which all download configuration files from the single master. (This orchestrating machine is not running the service at all.) The hosts are all in the same inventory, but belong to different groups (e.g.: [servers] and [client]).

We already have a handler to restart these services, when a local aspect of them changes.

But I can't figure out, how to notify the same handler -- on multiple machines -- when a config file changes on the master.

Is such a thing -- a notification of handlers on other hosts from the same inventory -- even possible with today's Ansible?

Upvotes: 2

Views: 1142

Answers (1)

larsks
larsks

Reputation: 311606

You're not going to be able to do that with a handler, but you could do it with an explicit play. E.g., let's say you have a playbook that targets your servers group and makes a change that requires something on your clients to restart. We could do something like this:

- hosts: servers
  gather_facts: false
  tasks:
    # this task could be anything. the important thing is
    # that it correctly reports a "changed" result and that
    # you register that in a variable.
    - command: cat /tmp/{{ inventory_hostname }}.status
      register: result
      changed_when: result.stdout == "changed"

    - set_fact:
        something_changed: "{{ result is changed }}"


- hosts: clients
  gather_facts: false
  tasks:
    - debug:
        msg: "restart all the things!"
      when: >-
        groups.servers |
        map('extract', hostvars, 'something_changed') |
        select() |
        list

On the servers, we register a something_changed variable that has a boolean value telling us whether or not something has changed.

On the clients, we check to see if any server reported a change, and if so, restart services or perform whatever other actions are necessary.

Upvotes: 3

Related Questions