Mastering Ansible: Capture Task Output with Register and Reuse It
This guide shows how to use Ansible's register feature to capture a task's output, examine the detailed result fields, and reuse the stored data in subsequent tasks, illustrated with a simple whoami example and complete playbook runs.
In Ansible you can capture a task's output with the register keyword, storing it in a variable for later use.
Below is a playbook that runs the whoami shell command, registers the result in the variable user, and then prints the variable with the debug module.
---
- name: Check the user
hosts: servera
tasks:
- name: View the logged in user name
shell: whoami
register: user
- debug:
var: userRunning the playbook produces the following output, where the debug task displays the captured result.
it@workstation:~/ansible$ ansible-playbook test.yml
BECOME password:
PLAY [Check the user] *****************************************************************
TASK [Gathering Facts] **************************************************************
ok: [servera]
TASK [View the logged in user name] *************************************************
changed: [servera]
TASK [debug] ***********************************************************************
ok: [servera] => {
"user": {
"changed": true,
"cmd": "whoami",
"delta": "0:00:00.002775",
"end": "2021-01-19 08:53:22.611335",
"failed": false,
"rc": 0,
"start": "2021-01-19 08:53:22.608560",
"stderr": "",
"stderr_lines": [],
"stdout": "root",
"stdout_lines": ["root"]
}
}
PLAY RECAP **************************************************************************
servera : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0The result dictionary contains several useful fields:
changed : true if the command was executed.
cmd : the command that was run.
delta : time taken to execute the command.
start : timestamp when the command started.
end : timestamp when the command finished.
failed : false indicates success; true indicates failure.
rc : return code (0 means success).
stderr and stderr_lines : standard error output.
stdout and stdout_lines : standard output.
You can reference any of these fields in later tasks. For example, to print a friendly message using the captured username:
---
- name: Check the user
hosts: servera
tasks:
- name: View the logged in user name
shell: whoami
register: user
- debug:
msg: "Logged in as user {{ user.stdout }}"Running this playbook yields:
it@workstation:~/ansible$ ansible-playbook test.yml
BECOME password:
PLAY [Check the user] *****************************************************************
TASK [Gathering Facts] **************************************************************
ok: [servera]
TASK [View the logged in user name] *************************************************
changed: [servera]
TASK [debug] ***********************************************************************
ok: [servera] => {"msg": "Logged in as user root"}
PLAY RECAP **************************************************************************
servera : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
