Python Operations¶
The Python module allows you to execute Python code within the context of a deploy.
python.call¶
Stateless operation
This operation will always execute commands and is not idempotent.
Execute a Python function within a deploy.
python.call(function: Callable, **kwargs)
function: the function to execute
args: arguments to pass to the function
kwargs: keyword arguments to pass to the function
Example:
def my_callback(hello=None):
    command = 'echo hello'
    if hello:
        command = command + ' ' + **hello**
    status, stdout, stderr = host.run_shell_command(command=command, sudo=SUDO)
    assert status is True  # ensure the command executed OK
    if 'hello ' not in '\n'.join(stdout):  # stdout/stderr is a *list* of lines
        raise Exception(
            f'`{command}` problem with callback stdout:{stdout} stderr:{stderr}',
        )
python.call(
    name="Run my_callback function",
    function=my_callback,
    hello="world",
)
- Note:
 This operation also inherits all global arguments.
python.raise_exception¶
Stateless operation
This operation will always execute commands and is not idempotent.
Raise a Python exception within a deploy.
python.raise_exception(exception: Exception, **kwargs)
exception: the exception class to raise
args: arguments passed to the exception creation
kwargs: keyword arguments passed to the exception creation
Example:
python.raise_exception(
    name="Raise NotImplementedError exception",
    exception=NotImplementedError,
    message="This is not implemented",
)
- Note:
 This operation also inherits all global arguments.
            pyinfra next