salt.states.python#

Execution of Python code and scripts using Salt's own interpreter#

The python state module runs Python code or scripts using the same interpreter that is running Salt, rather than whatever python/ python3 happens to resolve to on the target's PATH.

A simple example to execute a snippet of Python code:

write-marker-file:
  python.run:
    - name: open('/tmp/salt-marker', 'w').close()

Download and run a script with the running Salt interpreter:

run-my-script:
  python.script:
    - source: salt://scripts/runme.py
    - args: arg1 arg2
salt.states.python.run(name, args=None, cwd=None, runas=None, password=None, env=None, output_loglevel='debug', hide_output=False, timeout=None, ignore_timeout=False, use_vt=False, success_retcodes=None, success_stdout=None, success_stderr=None, **kwargs)#

Run a snippet of Python code, using the same interpreter that is running Salt, if certain circumstances are met.

name

The Python code to execute.

args

Additional arguments to pass to the interpreter (string or list). Only used if name should not be treated as the -c command, e.g. for -m module invocations.

cwd

The directory from which to execute the code. Defaults to the home directory of the user specified by runas (or the user under which Salt is running if runas is not specified).

runas

The user name (or uid) to run the code as.

password

Windows only. Required when specifying runas. This parameter will be ignored on non-Windows platforms.

env

A list of environment variables to be set prior to execution.

output_logleveldebug

Control the loglevel at which the output from the command is logged to the minion log.

hide_outputFalse

Suppress stdout and stderr in the state's results.

timeout

If the command has not terminated after timeout seconds, send the subprocess sigterm, and if sigterm is ignored, follow up with sigkill.

ignore_timeout

Ignore the timeout of commands, which is useful for running nohup processes.

use_vt

Use VT utils (saltstack) to stream the command output more interactively to the console and the logs. This is experimental.

success_retcodes

A list of non-zero return codes that should be considered a success.

success_stdout

A list of strings that when found in standard out should be considered a success.

success_stderr

A list of strings that when found in standard error should be considered a success.

salt.states.python.script(name, source=None, template=None, cwd=None, runas=None, password=None, env=None, timeout=None, use_vt=False, output_loglevel='debug', hide_output=False, defaults=None, context=None, success_retcodes=None, success_stdout=None, success_stderr=None, **kwargs)#

Download a Python script and execute it with the same interpreter that is running Salt.

source

The location of the script to download. If the file is located on the master in the directory named spam, and is called eggs, the source string is salt://spam/eggs.

name

Either "script arg1 arg2 arg3..." (if source is also given) or a source "salt://...".

template

If this setting is applied then the named templating engine will be used to render the downloaded file. Currently jinja, mako, and wempy are supported.

cwd

The directory from which to execute the script. Defaults to the home directory of the user specified by runas (or the user under which Salt is running if runas is not specified).

runas

Specify an alternate user to run the script as. The default behavior is to run as the user under which Salt is running.

password

Windows only. Required when specifying runas. This parameter will be ignored on non-Windows platforms.

env

A list of environment variables to be set prior to execution.

timeout

If the command has not terminated after timeout seconds, send the subprocess sigterm, and if sigterm is ignored, follow up with sigkill.

use_vt

Use VT utils (saltstack) to stream the command output more interactively to the console and the logs. This is experimental.

output_logleveldebug

Control the loglevel at which the output from the command is logged to the minion log.

hide_outputFalse

Suppress stdout and stderr in the state's results.

context

Overrides default context variables passed to the template.

defaults

Default context passed to the template.

success_retcodes

A list of non-zero return codes that should be considered a success.

success_stdout

A list of strings that when found in standard out should be considered a success.

success_stderr

A list of strings that when found in standard error should be considered a success.