0

Say I have a service on several servers, controled in a cluster way (heartbeat), disable (on startup) because I want it to run only on the active node:

haproxy:
  service.disabled: []

I want to be able to update its configuration file:

/etc/haproxy/haproxy.cfg:
  file.managed:
    - source: salt://haproxy.cfg
    - watch_in:
      - service: haproxy

By specifying that this file is watched by the haproxy service, it will trigger a service restart if this file is modified. That's what I would want, except that this service should be running only for the active server in the cluster.

So my question is: how can I achieve my goal to restart this service upon configuration modification, without starting it on all passive nodes?

MadHatter
  • 78,442
  • 20
  • 178
  • 229
Christophe Drevet
  • 1,962
  • 2
  • 17
  • 25

1 Answers1

1

The first question you have to answer is:

How do you know if the current node is active?

Then, instead of watch_in the service state, you can use cmd state with an onlyif condition, something like this:

haproxy:
  pkg:
    - installed
  file:
    - managed
    - name: /etc/haproxy/haproxy.cfg
    - source: salt://haproxy/config.cfg
    - require:
      - pkg: haproxy
    - watch_in:
      - cmd: haproxy
  cmd:
    - run
    - name: /etc/init.d/haproxy restart
    - onlyif: /this/node/is/the/active

Not fully tested. Please let me know if it works for you.

quanta
  • 50,327
  • 19
  • 152
  • 213