What is register?
register saves a task’s result into a variable for later tasks.
Task → register → variable → use later (often with debug)
---
- name: Register demo
hosts: all
tasks:
- name: Check uptime
command: uptime
register: uptime_output
changed_when: false
- name: Print output
debug:
var: uptime_output.stdoutansible-playbook playbooks/register.yml| Keyword | Purpose |
|---|---|
register | Store the result |
debug | Print it |
stdout | Command output field |
Common registered fields
| Field | Meaning |
|---|---|
stdout | Standard output |
stderr | Error output |
rc | Return code |
changed | Whether the task changed something |
Service status example
---
- name: Service status
hosts: all
tasks:
- name: Check nginx status
shell: systemctl is-active nginx
register: nginx_status
changed_when: false
failed_when: false
- name: Print nginx status
debug:
var: nginx_status.stdoutDebug patterns
- debug:
var: ansible_hostname
- debug:
msg: "Welcome to DevOps on {{ inventory_hostname }}"Use debug to troubleshoot playbooks and verify variables/facts. Remove noisy debug tasks before production.
Mini project — disk usage monitoring
---
- name: Disk usage monitoring
hosts: all
tasks:
- name: Check disk usage
command: df -h /
register: disk_output
changed_when: false
- name: Print disk usage
debug:
var: disk_output.stdoutWhat’s next (Day 3+)
register / debug
↓
handlers & notify
↓
tags, limits, strategies
↓
conditionals & loops
Set changed_when: false on read-only command tasks so Ansible does not report them as changes.