Ansible Module Return Value

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

Ansible modules return after execution a data structure that can be registered into a variable with the task's register: keyword. All modules return a set of common keys, documented below in the "Common Return Keys" section. Each module may choose to return keys specific to the module, described in the module's documentation.

For example, the execution of a command tasks produces the following return value map:

cmd:
 - "sh"
 - "-l"
 - "/tmp"
failed: false
stdout: "lrwxr-xr-x@ 1 root  admin  11 Sep 21  2020 /tmp -> private/tmp"
stdout_lines:
  - "lrwxr-xr-x@ 1 root  admin  11 Sep 21  2020 /tmp -> private/tmp"
stderr: ""
stderr_lines: []
start: "2021-07-03 23:39:57.499748"
end: "2021-07-03 23:39:57.506848"
changed: false
delta: "0:00:00.007100"
rc: 0

The content of execution stdout and stderr can be accessed with variable_name.stdout, variable_name.stderr, the failure situation with variable_name.failed, etc. A typical pattern is to use the task output accessible through registered variables and conditionally perform actions based on the registered variables values.

Common Return Keys

changed

A boolean indicating whether the task had to make changes to the target or delegate host.

failed

A boolean that indicates if the task was failed or not.

rc

Some modules execute command line utilities directly and this field contains the command's return code.utilities.

results

If the key exist, it indicates that a loop was present for the task and it contains a list of individual results, one per item.

skipped

A boolean that indicates if the task was skipped or not

stderr

Some modules execute command line utilities directly and this field contain the content of the stderr after execution, in this case.

stderr_lines

When stderr is returned, stderr_lines is also present. It contains a list of strings, one item per line from the original.

stdout

Some modules execute command line utilities directly and this field contain the content of the stdout after execution, in this case.

stdout_lines

When stdout is returned, stdout_lines is also present. It contains a list of strings, one item per line from the original.

backup_file

https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#backup-file

diff

https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#diff

invocation

https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#invocation

msg

https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#msg