Search code examples
salt-stack

Creating a Salt state that will be executed upon minion connection


I unable to find in the documentation an example of the following use case:

Minions, all of them or only a specific group - will apply some state on their own
upon system startup or restored connection..
Can I create a task in Salt that I do not need to manually apply with state.apply ?

I want to manage Windows boxes (not VMs) -
so not all of them are online at the time that I decide to apply some state..



Solution

  • To apply states on system/service restart you can set startup_states in the minion config:

    startup_states: highstate
    
    startup_states: sls
    sls_list:
      - minion_startup
    

    Alternatively, you can schedule a job to run every N seconds after the minion starts, which includes at startup by default:

    schedule:
      highstate:
        function: state.apply
        seconds: 3600
    

    If that's not what you mean by "restored connection", then you probably want to look at presence events and writing a reactor:

    presence_events: true
    reactor:
      - salt/presence/change:
        - salt://reactor/presence.sls
    
    {% for minion in data["new"] %}
    reconnect state:
      local.state.apply:
        - tgt: "{{ minion }}"
        - arg:
          - minion_startup
    {% endif %}