Search code examples
yamljinja2salt-project

How to force a salt state with onchanges based on a state in the 'include' section


I want to restart a service (haproxy) on a machine based on changes executed on another state. This would tipically be easy with onchanges but I have a particular case. Here's the state:

include:
  - linux.v1_0

(...)

ftp.service:
  service.running:
    - reload: True
    - watch:
      - <what to use here?>

I've seen people saying to use the watch in order to restart. Thing is, the linux.v1_0 state included will make some changes and I want to restart the haproxy service only anf if changes are actually applied by the state. I would like to avoid to explicitly use systemctl restart haproxy.

The linux.v1_0 state has the following:

{% set ssl = pillar.get("c") %}


{% for domain, cert in ssl.items() %}

cert.ssl.crt.{{ domain }}:
  file.managed:
    - name: etc/ssl/{{ domain }}
    (... more stuff ...)

{% endfor %}

So the task ID itself can vary from various domains, for example cert.ssl.crt.pt or cert.ssl.crt.es.

On the original state, I want the ftp service to restart ONLY when there are changes on the cert.ssl.crt.{{ domain }}task on the linux.v1_0 state.


Solution

  • To clarify, Salt requisites work on state ID. It does not matter whether a particular state ID is in found in included SLS or in-line. This can be checked with state.show_sls.

    With respect to the dynamic state IDs getting generated, we can use the _in variant of the watch requisite, i.e. watch_in.

    Like so:

    {% for domain, cert in ssl.items() %}
    cert.ssl.crt.{{ domain }}:
      file.managed:
        - name: etc/ssl/{{ domain }}
        (... more stuff ...)
        - watch_in:
            service: ftp.service
    {% endfor %}
    

    While it will work for this requirement, any other SLS that includes linux.v1_0 will have to define ftp.service:. Please try to keep linux.v1_0 self-sufficient with related service restarts/reloads if possible.