Saturday, 6 December 2025

Ansible 14. registering a variable

The Ansible register keyword is used to capture the output and status of a task and save it as a variable for use in subsequent tasks or conditional logic within the same playbook run.

This is essential when you need to use dynamic information generated by a command or module on a remote host.

[oracle@oel01db ansible-project]$ cat ./playbooks/register-demo.yml

---

- name: register demo

  hosts: db_servers

  tasks:

    - name: get the hostname

      shell: hostname

      register: result


    - name: print the hostname

      debug:

        msg: "{{ result }}"

[oracle@oel01db ansible-project]$


When you use register, the variable (e.g., result ) is created as a dictionary containing structured information about the task's execution, even if the task was just a simple command.

The key pieces of information you'll often extract from this dictionary are:


KeyDescriptionExample Access
stdoutThe standard output (the normal text result) of the command, as a single string.{{ system_info.stdout }}
stdout_linesThe standard output split into a list of strings, one for each line.{{ system_info.stdout_lines[0] }}
stderrThe standard error output of the command.{{ system_info.stderr }}
rcThe return code (exit code) of the command. 0 usually means success.{{ system_info.rc }}
changedA boolean (True or False) indicating if Ansible reported a change on the remote system.{{ system_info.changed }}

[oracle@oel01db ansible-project]$ ansible-playbook -i ./inventory/hosts ./playbooks/register-demo.yml


PLAY [register demo] ********************************************************************************************************************************************


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

ok: [192.168.0.156]


TASK [get the hostname] *****************************************************************************************************************************************

changed: [192.168.0.156]


TASK [print the hostname] ***************************************************************************************************************************************

ok: [192.168.0.156] => {

    "msg": {

        "changed": true,

        "cmd": "hostname",

        "delta": "0:00:00.030538",

        "end": "2025-12-06 00:28:14.643080",

        "failed": false,

        "rc": 0,

        "start": "2025-12-06 00:28:14.612542",

        "stderr": "",

        "stderr_lines": [],

        "stdout": "oel02db.mydb.com",

        "stdout_lines": [

            "oel02db.mydb.com"

        ]

    }

}


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

192.168.0.156              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[oracle@oel01db ansible-project]$

If you just want to capture the stdout 

[oracle@oel01db ansible-project]$ vi ./playbooks/register-demo.yml

[oracle@oel01db ansible-project]$ cat ./playbooks/register-demo.yml

---

- name: register demo

  hosts: db_servers

  tasks:

    - name: get the hostname

      shell: hostname

      register: result


    - name: print the hostname

      debug:

        msg: "{{ result.stdout }}"

[oracle@oel01db ansible-project]$

[oracle@oel01db ansible-project]$ ansible-playbook -i ./inventory/hosts ./playbooks/register-demo.yml


PLAY [register demo] *************************************************************************************************************************************************************************************


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

ok: [192.168.0.156]


TASK [get the hostname] **********************************************************************************************************************************************************************************

changed: [192.168.0.156]


TASK [print the hostname] ********************************************************************************************************************************************************************************

ok: [192.168.0.156] => {

    "msg": "oel02db.mydb.com"

}


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

192.168.0.156              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[oracle@oel01db ansible-project]$

 

No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...