This lab demonstrates how to design structured Ansible inventories and validate variable resolution using group and host variables. It also covers group hierarchy and execution across parent-child groups.
/home/user/inventory/
├── hosts.ini
├── group_vars/
│ └── server.yml
└── host_vars/
└── server1.ymlmkdir -p /home/user/inventory/group_vars
mkdir -p /home/user/inventory/host_vars[server]
server1 ansible_host=server1 ansible_user=server1_admin ansible_ssh_pass=server1_admin@123!
[webserver]
server1
[allservers:children]
webserverDefines:
- Host:
server1 - Group:
webserver - Parent group:
allservers
ansible_user: server1_admin
ansible_ssh_pass: server1_admin@123!Stored in: /home/user/inventory/group_vars/server.yml
Applies shared variables across groups.
http_port: 80Stored in: /home/user/inventory/host_vars/server1.yml
Overrides group-level variables for server1.
ansible -i /home/user/inventory/hosts.ini server1 \
-m debug -a "var=http_port" > http_port_result.txtExpected output in http_port_result.txt:
http_port: 80
Confirms host-level variables take precedence.
ansible -i /home/user/inventory/hosts.ini allservers \
-m command -a "uname -a" > allservers_uname.txt- Executes command on all child group hosts
- Output should contain system/kernel information from
server1
- Structured inventory using directories
- Group variables (
group_vars) - Host variables (
host_vars) - Variable precedence (host > group)
- Parent-child group relationships
- Output verification using redirection
This lab demonstrates how Ansible manages variables and group hierarchies in real-world scenarios, ensuring predictable configuration and scalable inventory design.