Debug Variable Precedence Issues

AnsibleBeginner
Practice Now

Introduction

When the same variable is defined in multiple places, Ansible applies precedence rules. Debugging the final value is essential before changing a playbook.

In this lab, you will define the same variables at all, group, host, and extra-var levels, then render reports that prove which level wins.

Create a Precedence Practice Project

In this step, you will create a project with all, group, and host 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

This lab intentionally defines the same variable in several places. The reports will show the final value Ansible selects rather than asking you to infer precedence from file names alone.

Define All and Group Variables

In this step, you will define baseline values in group_vars/all.yml, then override them for 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/all.yml <<'EOF'
app_port: 8000
app_source: all
EOF
cat > inventory/group_vars/web.yml <<'EOF'
app_port: 8080
app_source: group
EOF

group_vars/all.yml applies to every host, while group_vars/web.yml applies only to the web group. Because web1 and web2 are both web hosts, the group values should override the all-host baseline.

cat inventory/group_vars/all.yml inventory/group_vars/web.yml

Define a Host Variable Override

In this step, you will override the same variables only for 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'
app_port: 9090
app_source: host
EOF

This file applies only to web2, so it should override the web group values for that host. web1 should still use the group values.

ansible-inventory --host web2

Use the inventory output to confirm the resolved value before running a playbook. It should show app_port as 9090 for web2.

Render the Resolved Variable Values

In this step, you will write and run a playbook that records the final values Ansible chose for each 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/show-precedence.yml <<'EOF'
---
- name: Show resolved variable precedence
  hosts: web
  gather_facts: false
  tasks:
    - name: Write resolved variable report
      ansible.builtin.copy:
        dest: "/home/labex/project/rhce-ansible/reports/{{ inventory_hostname }}.txt"
        content: |
          host={{ inventory_hostname }}
          app_port={{ app_port }}
          app_source={{ app_source }}
        mode: '0644'
EOF

The playbook writes the final resolved values to files. This makes precedence visible: each report records both the chosen port and a source label that tells you which layer won.

ansible-playbook playbooks/show-precedence.yml | tee reports/normal-run.txt
cat reports/web1.txt reports/web2.txt

web1 should show app_source=group, and web2 should show app_source=host. That proves host variables are more specific than group variables.

Override with Extra Variables

In this step, you will pass extra variables with -e. Extra variables have very high precedence and override both group and host variables for this run. 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/show-precedence.yml -e app_port=10080 -e app_source=extra | tee reports/extra-run.txt

Extra variables are intentionally powerful. They override both group and host variables for this run, but they do not edit the variable files on disk.

cat reports/web1.txt reports/web2.txt

Both reports should now show the extra variable values. If only one host changed, recheck how the -e options were typed.

Summary

You compared all, group, host, and extra variables, then used rendered reports to prove which value Ansible applied at each precedence level.