Salt state to enable re-run systemd service

Viewed 131

I am trying to craft a salt state file to simply ensure-enabled and re-run my one-shot service. I thought it would be nice to re-run if any of the dependent files changed, but honestly this is simple enough and the short-lived service is almost never going to be running when I want to update.

Current attempt:

myown-systemd-service-unit-file:
...

myown-systemd-service-executable-file:
...

myown-service:
  systemd.force_reload:
    - name: myown
    - enable: True
    - watch:
      - myown-systemd-service-unit-file
      - myown-systemd-service-executable-file

is failing at with errror:

----------
          ID: myown-service
    Function: systemd.force_reload
        Name: myown
      Result: False
     Comment: State 'systemd.force_reload' was not found in SLS 'something.myown'
              Reason: 'systemd.force_reload' is not available.
     Changes: 

By enable, I mean to have the equivalent of this CLI call be applied:

sudo systemctl enable myown.service

Relevant docs: https://docs.saltproject.io/en/latest/ref/modules/all/salt.modules.systemd_service.html#module-salt.modules.systemd_service

1 Answers

The systemd_service module is an execution module, and the syntax to use such modules is slightly different. The state declaration you are using is for state modules. Also, the example from the documentation points to use of service.force_reload rather than systemd.force_reload.

salt '*' service.force_reload <service name>

Considering all this, the below example restarts and enables myown service when the service unit file changes.

myown-service:
  module.run:
  - service.restart:
    - name: myown
    onchanges:
    - file: myown-systemd-service-unit-file
  - service.enable:
    - name: myown

Note that I've used restart instead of force_reload to bounce the service. Also I'm using onchanges for file module as you haven't shown how you manage the two files. You can use the appropriate module and state IDs.

Related