MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Ansible Variable: Ansible & Ansible Tower Variable Precedence Hierarchy

When the same variable is defined in more than one place, Ansible uses a fixed precedence order and the source listed last wins. Extra vars passed with -e always win and role defaults are almost always overridden. Below is the complete order, how Ansible Tower/AWX adds its own layers on top, a small example you can run, and how to check which value Ansible actually used.

Ansible & Ansible Tower Variable Precedence Hierarchy (last listed wins)

Table comparing Ansible and Tower variable precedence, from role defaults up to extra variables


Ansible variable precedence (lowest to highest)

This is the order used by current ansible-core releases. A variable defined at a higher number overrides the same variable defined at a lower number:

  1. command line values such as -u my_user (these are not variables)
  2. role defaults (roles/x/defaults/main.yml)
  3. inventory file or script group vars
  4. inventory group_vars/all
  5. playbook group_vars/all
  6. inventory group_vars/*
  7. playbook group_vars/*
  8. inventory file or script host vars
  9. inventory host_vars/*
  10. playbook host_vars/*
  11. host facts and cached set_fact
  12. play vars
  13. play vars_prompt
  14. play vars_files
  15. role vars (roles/x/vars/main.yml)
  16. block vars (only for tasks in the block)
  17. task vars (only for that task)
  18. include_vars
  19. set_fact and registered vars
  20. role and include_role params
  21. include params
  22. extra vars, -e "user=my_user" (always win)

How Ansible Tower / AWX fits into this order

Tower (now Red Hat Ansible Automation Platform) and AWX run ansible-playbook for you, so all of the levels above still apply. On top of them they add their own sources:

  • Inventory, group and host variables entered in the Tower inventory behave like inventory variables, so they sit low in the order and are overridden by play, role and task variables.
  • Job template extra variables, survey answers and prompt-on-launch values are passed to Ansible as extra vars, so they override everything defined in playbooks, roles and inventories.
  • Among those Tower-level extra vars, values supplied when the job is launched override the extra vars saved on the job template, and extra vars set on a workflow override those of the job templates inside it.

This is why a value typed into a survey wins even if the playbook sets the same variable in vars:.

Example: see precedence in action

# inventory
[webservers]
web1 ansible_host=192.168.1.10

# roles/web/defaults/main.yml
http_port: 80

# group_vars/webservers.yml
http_port: 8080

# site.yml
- hosts: webservers
  roles:
    - web
  tasks:
    - name: Show which value won
      ansible.builtin.debug:
        var: http_portCode language: PHP (php)
$ ansible-playbook -i inventory site.yml
ok: [web1] => { "http_port": 8080 }     # group_vars beats role defaults

$ ansible-playbook -i inventory site.yml -e http_port=9090
ok: [web1] => { "http_port": "9090" }   # extra vars beat everythingCode language: PHP (php)

Note that -e key=value passes values as strings. Pass JSON, for example -e '{"http_port": 9090}', if you need a number, list or boolean.

How to find out which value Ansible is using

  • Print the variable in a task: ansible.builtin.debug: var=http_port.
  • Check a single host without a playbook: ansible -i inventory web1 -m ansible.builtin.debug -a "var=http_port".
  • See all inventory variables for a host: ansible-inventory -i inventory --host web1 (shows inventory sources only, not play or role vars).
  • Run with -vvv to see which files Ansible loads.

Common mistakes

  • Putting overridable values in roles/x/vars/main.yml. Role vars (level 15) beat inventory group_vars and host_vars, so users cannot override them from the inventory. Put values you expect people to change in defaults/main.yml.
  • Expecting group_vars to beat host_vars. Host variables always override group variables.
  • Two groups at the same level setting the same variable. Ansible merges sibling groups alphabetically, so the last group name wins. Set ansible_group_priority on a group to control this.
  • Expecting dictionaries to merge. By default a dictionary defined at a higher level replaces the whole dictionary, it does not merge keys.
  • Trying to override extra vars with set_fact. Nothing overrides extra vars, not even set_fact.

Rules of thumb

  • Role defaults for values users may change; role vars for internal constants.
  • Inventory group_vars for environment differences (dev, staging, prod).
  • Prefix role variables with the role name (web_http_port) to avoid accidental clashes.
  • Use -e or Tower surveys only for deliberate one-off overrides.

Frequently asked questions

Which variable has the highest precedence in Ansible?

Extra vars passed with -e (or from Tower job templates, surveys and launch prompts). They override every other source.

Which has the lowest precedence?

Role defaults. Command line options such as -u are listed below them, but they are connection settings rather than variables.

Can I change the precedence order?

No. You can only control which group wins between groups at the same level with ansible_group_priority.

Do Tower survey answers override playbook vars?

Yes. Survey answers are passed as extra vars, so they override vars: in the playbook, role vars and inventory variables.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Ansible tower job template vs workflow template

Ansible Tower job templates and workflow templates are both used to automate tasks, but they have different purposes. Here is a table that summarizes the key differences…

Read More

Ansible Variable: Variables used in Ansible Vs Tower

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

How Ansible Automates Cisco IOS and IOS XE Network Devices

Introduction Managing multiple Cisco routers and switches manually can become time-consuming as a network grows. Network engineers regularly configure interfaces, check device status, update settings, and troubleshoot…

Read More

Configuration Management in DevOps: Concepts, Tooling, and a Practical Workflow

This guide explains how configuration management works in modern DevOps teams, compares common tools, and shows a practical Ansible example. It also preserves the original SlideShare reference from the legacy post.

Read More

Configuration Management in DevOps: Workflow, Tool Comparison, and an Ansible Example

This guide explains configuration management for DevOps teams: what it solves, how it fits into delivery workflows, and how major tool options compare. It also includes a practical Ansible example you can adapt for real environments.

Read More

Infrastructure as Code: A Practical Guide for DevOps Professionals

Introduction Managing infrastructure has undergone a massive transformation over the past decade. If you started your career in the era of physical data centers, you likely remember…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x