Lab activity
Ansible Facts and Registered Variables
Learn how to use Ansible facts and registered variables to build dynamic playbooks that make decisions based on host information and task output.
Overview
In this lab, you will learn how to use Ansible facts and registered variables to create dynamic and intelligent playbooks. You will display automatically gathered system information, capture the output of commands using the register keyword, and use conditional logic to execute tasks based on the captured results. By the end of this lab, you will be able to leverage host-specific data and task outputs to build more flexible and automated Ansible playbooks.
Learning Outcomes
- Understand how Ansible automatically gathers system facts from managed hosts.
- Learn to capture task output using registered variables.
- Use Ansible facts and registered variables within playbooks and debug tasks.
- Implement conditional task execution using the when statement.
- Verify task output and create resources based on conditional logic.
Key Concepts
- Ansible Facts: Automatically collected system information such as hostname, operating system, memory, and network details.
- Registered Variables: Variables created using the register keyword to store the output of a task for later use.
- Debug Module: Displays the values of variables, facts, and registered outputs during playbook execution.
- Conditional Execution: Uses the when statement to run tasks only when specified conditions are met.
- Command Output: The result of executing a command, which can be reused throughout the playbook using registered variables.
Why Needed?
- Enables Dynamic Automation: Allows playbooks to adapt automatically to the configuration of each managed host.
- Supports Intelligent Decision-Making: Uses command results and system facts to determine which tasks should run.
- Reduces Manual Effort: Eliminates the need to manually collect and process system information.
- Improves Playbook Flexibility: Reuses task outputs across multiple tasks without executing commands repeatedly.
- Enhances Automation Reliability: Ensures tasks execute only when the required conditions are satisfied.