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

This assumes you have installed Ansible, are familiar with the ansible-playbook command and the shell command, and have created a playbook.

The register module 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 command will be stored in a variable named "out". You can then do something with the register, such as printing the output and return code.

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

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

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

    - name: return code
      debug: 
        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).

PLAY [all]

TASK [Gathering Facts]
ok: [server1.example.com]

TASK [list command]
ok: [server1.example.com]

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

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

TASK [return code]
ok: [server1.example.com] => {
    "msg": "0"
}

PLAY RECAP
server1.example.com : ok=1  changed=0  unreacable=0  failed=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 59a43 in the box below so that we can be sure you are a human.




Comments