FreeKB - Ansible register parameter (stdout stderr rc)
Ansible - register parameter (stdout stderr rc)

The register parameter can be used to store the standard output (stdout), standard error (stderr) and return code (rc) of a command. In this example, the stdout/stderr/rc of the ls (list) command will be stored in a variable named "out". 

- name: list command
  shell: "ls /tmp"
  register: out


You can then do something with the register, such as printing the output using the debug module, like this. Notice the double curley braces {{ ... }}. Jinja2 uses double curley braces for variables.

- name: standard out
    msg: "{{ out.stdout_lines }}"

- name: standard error
    msg: "{{ out.stderr_lines }}"

- name: return code
    msg: "{{ out.rc }}"


Running this playbook should return the following. In this example, standard out returns a file or directory in the /tmp directory named "foo", there was no standard error, and the return code is 0 (success).

TASK [list command]
ok: []

TASK [standard out]
ok: [] => {
    "msg": "foo"

TASK [standard error]
ok: [] => {
    "msg": ""

TASK [return code]
ok: [] => {
    "msg": "0"


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 07d09 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |