Register in ansible

Ansible register is a way to capture the output from task execution and store it in a variable. This is an important feature, as this output is different for each remote host, and the basis on that we can use conditions loops to do some other tasks. Also, each register value is valid throughout the playbook execution.

We can use the register module to run debug.

Let's create a playbook for this,

# cat echo.yaml

---

- hosts: all

  tasks:

  - name: print stdoutZippyOPS

    command: echo "hello there"

    register: hello

  - debug: msg="stdout={{ hello.stdout }}"

  - debug: msg="stderr={{ hello.stderr }}"


Tasks are to print standard output, the command is “hello there”

My register name is “hello “

-debug studout is used to get stand output in that registry to get echo command ouput,

Next is debug standard error found to run a command.

Let to run playbook,

# ansible-playbook echo.yaml

PLAY [all] **************************************************************************************************************************************************


TASK [Gathering Facts] **************************************************************************************************************************************

ok: [192.168.1.27]


TASK [print stdoutZippyOPS] *********************************************************************************************************************************

changed: [192.168.1.27]


TASK [debug] ************************************************************************************************************************************************

ok: [192.168.1.27] => {

    "msg": "stdout=hello there"

}


TASK [debug] ************************************************************************************************************************************************

ok: [192.168.1.27] => {

    "msg": "stderr="

}


PLAY RECAP **************************************************************************************************************************************************

192.168.1.27               : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


let’s add one more thing in that echo.yml file,

# cat echo.yaml

---

- hosts: all

  tasks:

  - name: print stdout

    command: echo "hello there"

    register: hello

  - debug: msg="stdout={{ hello.stdout }}"

  - debug: msg="stderr={{ hello.stderr }}"

  - debug:

      msg: "system {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}"


Now I have to run playbook,

# ansible-playbook echo.yaml

PLAY [all] **************************************************************************************************************************************************


TASK [Gathering Facts] **************************************************************************************************************************************

ok: [192.168.1.27]


TASK [print stdout] *****************************************************************************************************************************************

changed: [192.168.1.27]


TASK [debug] ************************************************************************************************************************************************

ok: [192.168.1.27] => {

    "msg": "stdout=hello there"

}


TASK [debug] ************************************************************************************************************************************************

ok: [192.168.1.27] => {

    "msg": "stderr="

}


TASK [debug] ************************************************************************************************************************************************

ok: [192.168.1.27] => {

    "msg": "system 192.168.1.27 has gateway 192.168.1.1"

}


PLAY RECAP **************************************************************************************************************************************************

192.168.1.27               : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



Relevant Blogs:

Blockinfile module in ansible 

custom log in ansible 

Ansible dictionary variables 

Loops in Jenkins pipeline

Recent Comments

No comments

Leave a Comment