The cmd state module manages the enforcement of executed commands, this state can tell a command to run under certain circumstances.
A simple example to execute a command:
date > /tmp/salt-run: cmd: - run
Only run if another execution failed, in this case truncate syslog if there is no disk space:
> /var/log/messages:
cmd.run:
- unless: echo 'foo' > /tmp/.test
Note that when executing a command or script, the state (i.e., changed or not) of the command is unknown to Salt's state system. Therefore, by default, the cmd state assumes that any command execution results in a changed state.
This means that if a cmd state is watched by another state then the state that's watching will always be executed due to the changed state in the cmd state.
Many state functions in this module now also accept a stateful argument. If stateful is specified to be true then it is assumed that the command or script will determine its own state and communicate it back by following a simple protocol described below:
If there's nothing in the stdout of the command, then assume no changes. Otherwise, the stdout must be either in JSON or its last non-empty line must be a string of key=value pairs delimited by spaces(no spaces on the sides of =).
If it's JSON then it must be a JSON object (e.g., {}). If it's key=value pairs then quoting may be used to include spaces. (Python's shlex module is used to parse the key=value string)
Two special keys or attributes are recognized in the output:
changed: bool (i.e., 'yes', 'no', 'true', 'false', case-insensitive) comment: str (i.e., any string)So, only if 'changed' is true then assume the command execution has changed the state, and any other key values or attributes in the output will be set as part of the changes.
If there's a comment then it will be used as the comment of the state.
Here's an example of how one might write a shell script for use with a stateful command:
#!/bin/bash # echo "Working hard..." # writing the state line echo # an empty line here so the next line will be the last. echo "changed=yes comment='something has changed' whatever=123"And an example salt file using this module:
Run myscript: cmd.run: - name: /path/to/myscript - cwd: / - stateful: true Run only if myscript changed something: cmd.wait: - name: echo hello - cwd: / - watch: - cmd: Run myscriptNote that if the cmd.wait state also specifies stateful: true it can then be watched by some other states as well.
cmd.wait is not restricted to watching only cmd states. For example it can also watch a git state for changes
# Watch for changes to a git repo and rebuild the project on updates
my-project:
git.latest:
- name: [email protected]/repo/foo
- target: /opt/foo
- rev: master
cmd.wait:
- name: make install
- cwd: /opt/foo
- watch:
- git: my-project
Invoke a pre-defined Python function with arguments specified in the state declaration. This function is mainly used by the salt.renderers.pydsl renderer.
The interpretation of onlyif and unless arguments are identical to those of salt.states.cmd.run(), and all other arguments(cwd, runas, ...) allowed by cmd.run are allowed here, except that their effects apply only to the commands specified in onlyif and unless rather than to the function to be invoked.
In addition the stateful argument has no effects here.
The return value of the invoked function will be interpreted as follows.
If it's a dictionary then it will be passed through to the state system, which expects it to have the usual structure returned by any salt state function.
Otherwise, the return value(denoted as result in the code below) is expected to be a JSON serializable object, and this dictionary is returned:
{ 'changes': { 'retval': result },
'result': True if result is None else bool(result),
'comment': result if isinstance(result, basestring) else ''
}
Execute a cmd function based on a watch call
Run a command if certain circumstances are met
Download a script from a remote source and execute it. The name can be the source or the source value can be defined.
Run the given command only if the watch statement calls it
Download a script from a remote source and execute it only if a watch statement calls it.