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 ps (show processes) command will be stored in a variable named "out". 

- name: ps command
  shell: ps
  register: out

 

You can then do something with the register, such as printing the output using the debug module.

- name: standard out
  debug: 
    var: out.stdout_lines

- name: standard error
  debug: 
    var: out.stderr_lines

- name: return code
  debug: 
    var: out.rc

 

Running this playbook should return something like this.

TASK [standard out]
ok: [server1.example.com] => {
    "msg": "4012  pts/2  00:00:00  bash"
}

TASK [standard error]
ok: [server1.example.com] => {
    "msg": ""
}

TASK [return code]
ok: [server1.example.com] => {
    "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 a25f1 in the box below so that we can be sure you are a human.




Comments


January 12th, 2022 by Todd
How do you count how many times the word foo appears in the stdout?


January 12th, 2022 by Jeremy (moderator)
The length filter can be used to count the number of elements in an item. Here is my article on the length filter - http://www.freekb.net/Article?id=2969


January 22nd, 2022 by Mijanur
Helpful one, thanks!

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |