13

We deploy and change system service files via SaltStack.

For example, if file /etc/systemd/system/superfoo.service gets changed, then systemd emits this warning:

Warning: Unit file of superfoo.service changed on disk, 
         'systemctl --system daemon-reload' recommended.

How to automate this with salt-stack?

guettli
  • 3,113
  • 14
  • 59
  • 110

3 Answers3

9

I'd like to provide more complete solution although @MaksaSila did answer first.

You just need a cmd.run that'll handle file change, I'm using similar approach:

# sample.sls

systemd-reload:
  cmd.run:
   - name: systemctl --system daemon-reload
   - onchanges:  
     - file: superbar.service

superbar.service:
  file.managed:
    - name: /etc/systemd/system/superbar.service

superfoo.service:
  file.managed:
    - name: /etc/systemd/system/superfoo.service
    - onchanges_in:
       - cmd: systemd-reload

The latter approach will allow you to split the systemd part and the service part into different SLS files (don't forget to include systemd for every sls file you do onchanges_in in).

See this manual page to get more details on the state relations and dependencies.

jollyroger
  • 1,650
  • 11
  • 19
7

@jollyroger's answer is good for version < 0.15.0

Starting from 0.15.0, we can make use of systemd_service.systemctl_reload: https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.systemd_service.html#salt.modules.systemd_service.systemctl_reload

superbar.service
  file.managed:
    - name: /etc/systemd/system/superbar.service
  module.run: 
    - name: service.systemctl_reload
    - onchanges:
      - file: /etc/systemd/system/superbar.service

I believe this greatly simplifies the code.

[Edit] The directive "systemctl_reload" might look like systemctl reload, but it executes systemctl --system daemon-reload underneath. https://github.com/saltstack/salt/blob/9bbbd3629418b2b086360f5b303323ec55ca0315/salt/modules/systemd_service.py#L377-L399

Saltstack doc also clearly states that it

"Reloads systemctl, an action needed whenever unit files are updated."

clony
  • 71
  • 1
  • 2
  • Looks good. Thank you. But if "systemctl_reload" does "daemon-reload", why did the salt devs not name the function like this? – guettli Nov 05 '19 at 09:49
  • 2
    I don't know ¯\_(ツ)_/¯. When I first looked at it, the name confused me too. – clony Nov 08 '19 at 07:15
0

I would suggest after coping this file to run this in your state:

update-systemd:
  cmd.run:
    - name: systemctl --system daemon-reload
MaksaSila
  • 76
  • 3
  • This always does daemon-reload. This is not what I want. I want it only to get executed if necessary. – guettli Nov 05 '19 at 09:46