salt-project

How to restart a systemd service with salt?


I am trying to build an .sls file which will always restart a service:

systemd-resolved:
  service.running:
    - restart: True

When deployed, this gives


      ID: systemd-resolved
Function: service.running
  Result: True
 Comment: The service systemd-resolved is already running
 Started: 23:46:49.999789
Duration: 53.068 ms
 Changes:

This is correct, the service is already running. What I was trying to convey with this command is to restart it. How to do that?

Note: I would like to avoid, if possible, an explicit command to be ran (as I feel it i snot very salt-like - this should rather be handled by the appropriate module):

'systemctl restart systemd-resolved':
  cmd.run

Solution

  • If you want your service to reload you need to set reload: True instead. Beside, If you only want to restart the service if there is any change in any other state, you need to use watch instead. for instance,

    systemd-resolved:
      service.running:
        - enable: True
        - reload: True
        - watch:
          - pkg: <abc>