Ansible in a Minute · Ansible Core ·
Use register to capture task output
Short answer
Use register to store a task result, then read fields such as stdout in a later task.
Subscribe for updates
Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.
By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.
Request
run echo hello, register the result as hello_result, then print hello_result.stdout
Example
A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.
---
- name: Capture command output with register
hosts: all
gather_facts: false
tasks:
- name: Run a command and capture the output
ansible.builtin.command:
cmd: echo hello
register: hello_result
- name: Display the command output
ansible.builtin.debug:
msg: "{{ hello_result.stdout }}"How it works
register stores the full task result in hello_result. Later tasks read fields from that variable. hello_result.stdout is hello. Each module documents its return values.
When to use it
- Using command output in a later when condition
- Passing a registered path or status into the next task
Requirements
- Ansible: >=2.14
Validation
- Syntax validated
- Not execution-tested
- Last verified 2026-10-11
Notes
You can use a registered variable in any later task in the same play. Run the playbook with -v to inspect the result. The shape of the result depends on the module. Do not name a variable _task. That name is reserved.
Reference
- Registering variables (Ansible docs)
Built for every Ansible use case in every organization
BacklogZero can generate this kind of automation in your environment, just like it did here.