SaltStack: Execute `systemctl --system daemon-reload` if service file was changed
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?
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'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."