Introduction
Variables let you keep data outside task logic. Group variables provide shared values, host variables override individual hosts, and facts add discovered system data.
In this lab, you will define group and host variables, gather RHEL facts, and render one report per host alias.
Create a Variable Practice Project
In this step, you will create a project with web host aliases and variable directories.
cd /home/labex/project
mkdir -p rhce-ansible/{inventory/group_vars,inventory/host_vars,playbooks,reports}
cd rhce-ansible
cat > ansible.cfg <<'EOF'
[defaults]
inventory = ./inventory/hosts
host_key_checking = False
retry_files_enabled = False
EOF
cat > inventory/hosts <<'EOF'
[web]
web1 ansible_connection=local
web2 ansible_connection=local
EOF
Both hosts are local aliases, but Ansible still resolves variables separately for each inventory name. That lets you practice host-specific overrides without additional VMs.
Set Shared Group Variables
In this step, you will define values that apply to every host in the web group.
Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.
cd /home/labex/project/rhce-ansible
cat > inventory/group_vars/web.yml <<'EOF'
service_name: demo-web
service_port: 8080
EOF
These values apply to every host in the web group. At this point both web1 and web2 would use port 8080.
cat inventory/group_vars/web.yml
Override One Host Variable
In this step, you will override service_port for only web2. Host variables are more specific than group variables.
Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.
cd /home/labex/project/rhce-ansible
cat > inventory/host_vars/web2.yml <<'EOF'
service_port: 9090
EOF
This file is more specific than group_vars/web.yml, so only web2 should use 9090. web1 should continue using the group value.
ansible-inventory --host web2
Inspect the resolved host output and look for service_port. This command shows the final value Ansible will use before any playbook runs.
Write a Fact-Aware Report Playbook
In this step, you will write a playbook that combines inventory variables with gathered facts. The fact ansible_distribution_major_version comes from the managed host.
Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.
cd /home/labex/project/rhce-ansible
cat > playbooks/report-vars.yml <<'EOF'
---
- name: Render variable and fact reports
hosts: web
gather_facts: true
tasks:
- name: Write per-host variable report
ansible.builtin.copy:
dest: "/home/labex/project/rhce-ansible/reports/{{ inventory_hostname }}.txt"
content: |
host={{ inventory_hostname }}
service={{ service_name }}
port={{ service_port }}
rhel_major={{ ansible_distribution_major_version }}
mode: '0644'
EOF
The report combines two data sources: inventory variables such as service_port, and gathered facts such as ansible_distribution_major_version. This is a common pattern when configuration depends on both declared intent and discovered system details.
ansible-playbook playbooks/report-vars.yml --syntax-check
Run the Variable Report
In this step, you will run the playbook and compare the reports. The shared values should match, while the port differs for web2. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.
cd /home/labex/project/rhce-ansible
ansible-playbook playbooks/report-vars.yml | tee reports/report-run.txt
The run should gather facts first, then write a report for each host. A successful recap with failed=0 means both host aliases rendered their reports.
cat reports/web1.txt reports/web2.txt
Compare the port= lines: web1 should show 8080, while web2 should show 9090. The shared service= value should match in both reports.
Summary
You defined group variables, overrode a value with host variables, gathered facts, and rendered reports that prove how Ansible resolved each value.


