Skip to content

Latest commit

 

History

History
119 lines (79 loc) · 2.28 KB

File metadata and controls

119 lines (79 loc) · 2.28 KB

Ansible Use Case: Node Testing and Variable Precedence

Overview

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.


Project Structure

/home/user/inventory/
├── hosts.ini
├── group_vars/
│   └── server.yml
└── host_vars/
    └── server1.yml

Implementation Steps

1. Create Inventory Structure

mkdir -p /home/user/inventory/group_vars
mkdir -p /home/user/inventory/host_vars

2. Create Inventory File

[server]
server1 ansible_host=server1 ansible_user=server1_admin ansible_ssh_pass=server1_admin@123!

[webserver]
server1

[allservers:children]
webserver

Defines:

  • Host: server1
  • Group: webserver
  • Parent group: allservers

3. Create Group Variables

ansible_user: server1_admin
ansible_ssh_pass: server1_admin@123!

Stored in: /home/user/inventory/group_vars/server.yml Applies shared variables across groups.


4. Create Host Variables

http_port: 80

Stored in: /home/user/inventory/host_vars/server1.yml Overrides group-level variables for server1.


5. Verify Variable Precedence

ansible -i /home/user/inventory/hosts.ini server1 \
  -m debug -a "var=http_port" > http_port_result.txt

Expected output in http_port_result.txt:

http_port: 80

Confirms host-level variables take precedence.


6. Verify Parent Group Execution

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

Key Concepts

  • Structured inventory using directories
  • Group variables (group_vars)
  • Host variables (host_vars)
  • Variable precedence (host > group)
  • Parent-child group relationships
  • Output verification using redirection

Summary

This lab demonstrates how Ansible manages variables and group hierarchies in real-world scenarios, ensuring predictable configuration and scalable inventory design.