File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/salt/states/__pycache__/cmd.cpython-310.pyc
Back
o ;j� � @ sP d Z ddlZddlZddlZddlZddlZddlZddlZddl m Z mZ e�e �Zdd� Zdd� Zdd � Z dd d�Zejj�ed�Z ddd�Z ddd�Z ddd�Z d dd�Z d!dd�Zdd� ZdS )"a$ Execution of arbitrary commands =============================== 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: .. code-block:: yaml # Store the current date in a file 'date > /tmp/salt-run': cmd.run Only run if another execution failed, in this case truncate syslog if there is no disk space: .. code-block:: yaml '> /var/log/messages/': cmd.run: - unless: echo 'foo' > /tmp/.test && rm -f /tmp/.test Only run if the file specified by ``creates`` does not exist, in this case touch /tmp/foo if it does not exist: .. code-block:: yaml touch /tmp/foo: cmd.run: - creates: /tmp/foo ``creates`` also accepts a list of files, in which case this state will run if **any** of the files do not exist: .. code-block:: yaml "echo 'foo' | tee /tmp/bar > /tmp/baz": cmd.run: - creates: - /tmp/bar - /tmp/baz .. note:: The ``creates`` option was added to the cmd state in version 2014.7.0, and made a global requisite in 3001. Sometimes when running a command that starts up a daemon, the init script doesn't return properly which causes Salt to wait indefinitely for a response. In situations like this try the following: .. code-block:: yaml run_installer: cmd.run: - name: /tmp/installer.bin > /dev/null 2>&1 Salt determines whether the ``cmd`` state is successfully enforced based on the exit code returned by the command. If the command returns a zero exit code, then salt determines that the state was successfully enforced. If the script returns a non-zero exit code, then salt determines that it failed to successfully enforce the state. If a command returns a non-zero exit code but you wish to treat this as a success, then you must place the command in a script and explicitly set the exit code of the script to zero. Please note that the success or failure of the state is not affected by whether a state change occurred nor the stateful argument. 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. .. _stateful-argument: Using the "Stateful" Argument ----------------------------- 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: 1. :strong:`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 either side of ``=``). 2. :strong:`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. 3. :strong:`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: .. code-block:: bash #!/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 SLS file using this module: .. code-block:: yaml Run myscript: cmd.run: - name: /path/to/myscript - cwd: / - stateful: True Run only if myscript changed something: cmd.run: - name: echo hello - cwd: / - onchanges: - cmd: Run myscript Note that if the second ``cmd.run`` state also specifies ``stateful: True`` it can then be watched by some other states as well. 4. :strong:`The stateful argument can optionally include a test_name parameter.` This is used to specify a command to run in test mode. This command should return stateful data for changes that would be made by the command in the name parameter. .. versionadded:: 2015.2.0 .. code-block:: yaml Run myscript: cmd.run: - name: /path/to/myscript - cwd: / - stateful: - test_name: /path/to/myscript test Run masterscript: cmd.script: - name: masterscript - source: salt://path/to/masterscript - cwd: / - stateful: - test_name: masterscript test Should I use :mod:`cmd.run <salt.states.cmd.run>` or :mod:`cmd.wait <salt.states.cmd.wait>`? -------------------------------------------------------------------------------------------- .. note:: Use :mod:`cmd.run <salt.states.cmd.run>` together with :ref:`onchanges <requisites-onchanges>` instead of :mod:`cmd.wait <salt.states.cmd.wait>`. These two states are often confused. The important thing to remember about them is that :mod:`cmd.run <salt.states.cmd.run>` states are run each time the SLS file that contains them is applied. If it is more desirable to have a command that only runs after some other state changes, then :mod:`cmd.wait <salt.states.cmd.wait>` does just that. :mod:`cmd.wait <salt.states.cmd.wait>` is designed to :ref:`watch <requisites-watch>` other states, and is executed when the state it is watching changes. Example: .. code-block:: yaml /usr/local/bin/postinstall.sh: cmd.wait: - watch: - pkg: mycustompkg file.managed: - source: salt://utils/scripts/postinstall.sh mycustompkg: pkg.installed: - require: - file: /usr/local/bin/postinstall.sh ``cmd.wait`` itself do not do anything; all functionality is inside its ``mod_watch`` function, which is called by ``watch`` on changes. The preferred format is using the :ref:`onchanges Requisite <requisites-onchanges>`, which works on ``cmd.run`` as well as on any other state. The example would then look as follows: .. code-block:: yaml /usr/local/bin/postinstall.sh: cmd.run: - onchanges: - pkg: mycustompkg file.managed: - source: salt://utils/scripts/postinstall.sh mycustompkg: pkg.installed: - require: - file: /usr/local/bin/postinstall.sh How do I create an environment from a pillar map? ------------------------------------------------- The map that comes from a pillar can be directly consumed by the env option! To use it, one may pass it like this. Example: .. code-block:: yaml printenv: cmd.run: - env: {{ salt['pillar.get']('example:key', {}) }} � N)�CommandExecutionError�SaltRenderErrorc C s� | d }i | d<