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:
| Key | Description | Example Access |
stdout | The standard output (the normal text result) of the command, as a single string. | {{ system_info.stdout }} |
stdout_lines | The standard output split into a list of strings, one for each line. | {{ system_info.stdout_lines[0] }} |
stderr | The standard error output of the command. | {{ system_info.stderr }} |
rc | The return code (exit code) of the command. 0 usually means success. | {{ system_info.rc }} |
changed | A 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