- 1 year ago
Saltstack action on state change
In SaltStack, you can perform specific actions or execute custom code when a state changes using Salt Reactor. The Salt Reactor allows you to define reactions or event-driven actions based on state changes in your Salt environment.
Here's a high-level overview of how you can use Salt Reactor to trigger actions when a state changes:
Configure Salt Reactor: Open the Salt Master configuration file (
/etc/salt/master
orC:\salt\conf\master
) and enable the reactor system. Uncomment or add the following lines:javascriptreactor:
- 'salt/minion/*/state/changed':
- /srv/reactor/reactor.sls
This configuration sets up the reactor to trigger the
reactor.sls
file whenever a minion's state changes.Create a Reactor File: Create the reactor file (
reactor.sls
) at the path specified in the master configuration. For example,/srv/reactor/reactor.sls
. In this file, define the actions or custom code you want to execute when a state changes. Here's an example:yaml{% if data['fun'] == 'state.highstate' %}
{% set changed_states = data['data']['changes']|keys() %}
{% if 'my_state_name' in changed_states %}
my_custom_action:
runner.my_custom_runner_function:
- param1: value1
- param2: value2
{% endif %}
{% endif %}
In this example, the reactor file checks if the state change is related to the
state.highstate
function and if themy_state_name
state has changed. If the condition is met, it triggers therunner.my_custom_runner_function
with the specified parameters.Customize the reactor file to perform the desired actions based on your requirements.
Restart Salt Master: After configuring the reactor and creating the reactor file, restart the Salt Master service to apply the changes.
- On Linux:
systemctl restart salt-master
- On Windows:
net stop salt-master && net start salt-master
- On Linux:
With these steps, when a state change occurs on a minion, Salt Reactor will trigger the defined actions or execute custom code based on your reactor file's configuration.
Make sure to consult the SaltStack documentation for more details on Salt Reactor and advanced configuration options.