Search code examples
salt-project

How to trigger an action upon change of state?


I am testing salt as a management system, using ansible so far.

How can I trigger an action (specifically, a service reload) when a state has changed?

In Ansible this is done via notify but browsing salt documentation I cannot find anything similar.

  • I found watch, which works the other way round: "check something, and if it changed to this and that".

  • there is also listen which seems to be closer to my needs (the documentation mentions a service reload) but I cannot put together the pieces.

To set an example, how the following scenario would work in salt: check a git repo (= create it if not existing or pull from it otherwise) and if it has changed, reload a service? The Ansible equivalent is

- name: clone my service
  git:
    clone: yes
    dest: /opt/myservice
    repo: http://git.example.com/myservice.git
    version: master
    force: yes
  notify:
    - restart my service if needed

- name: restart my service if needed
  systemd:
    name: myservice
    state: restarted
    enabled: True
    daemon_reload: yes

Solution

  • Your example:

    ensure my service:
      git.latest:
        - name: http://git.example.com/myservice.git
        - target: /opt/myservice
      service.running:
        - watch:
          - git: http://git.example.com/myservice.git
    

    When there will be change in repo (clone for the first time, update etc.) the state will be marked as "having changes" thus the dependent states - service.running in this case - will require changes, for service it means to restart

    What you are asking is covered in salt quickstart