{"id":5300,"date":"2018-09-06T07:16:03","date_gmt":"2018-09-06T07:16:03","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=5300"},"modified":"2026-11-21T19:01:49","modified_gmt":"2026-11-21T19:01:49","slug":"ansible-ansible-tower-variable-precedence-hierarchy","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/ansible-ansible-tower-variable-precedence-hierarchy\/","title":{"rendered":"Ansible Variable: Ansible &#038; Ansible Tower Variable Precedence Hierarchy"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When the same variable is defined in more than one place, Ansible uses a fixed precedence order and the source listed <strong>last wins<\/strong>. Extra vars passed with <code>-e<\/code> 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.<\/p>\n\n\n<p><strong>Ansible &amp; Ansible Tower Variable Precedence Hierarchy (last listed wins)<\/strong><\/p>\n<p><a href=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2018\/09\/Architecture-Tower_Variable_Precedence_Hierarchy.png\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5301\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2018\/09\/Architecture-Tower_Variable_Precedence_Hierarchy.png\" alt=\"Table comparing Ansible and Tower variable precedence, from role defaults up to extra variables\" width=\"525\" height=\"534\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/Architecture-Tower_Variable_Precedence_Hierarchy.png 525w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/Architecture-Tower_Variable_Precedence_Hierarchy-295x300.png 295w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/a><\/p>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_33108\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_27387\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_33108\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Ansible variable precedence (lowest to highest)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>command line values such as <code>-u my_user<\/code> (these are not variables)<\/li>\n\n\n<li>role defaults (<code>roles\/x\/defaults\/main.yml<\/code>)<\/li>\n\n\n<li>inventory file or script group vars<\/li>\n\n\n<li>inventory <code>group_vars\/all<\/code><\/li>\n\n\n<li>playbook <code>group_vars\/all<\/code><\/li>\n\n\n<li>inventory <code>group_vars\/*<\/code><\/li>\n\n\n<li>playbook <code>group_vars\/*<\/code><\/li>\n\n\n<li>inventory file or script host vars<\/li>\n\n\n<li>inventory <code>host_vars\/*<\/code><\/li>\n\n\n<li>playbook <code>host_vars\/*<\/code><\/li>\n\n\n<li>host facts and cached <code>set_fact<\/code><\/li>\n\n\n<li>play <code>vars<\/code><\/li>\n\n\n<li>play <code>vars_prompt<\/code><\/li>\n\n\n<li>play <code>vars_files<\/code><\/li>\n\n\n<li>role vars (<code>roles\/x\/vars\/main.yml<\/code>)<\/li>\n\n\n<li>block vars (only for tasks in the block)<\/li>\n\n\n<li>task vars (only for that task)<\/li>\n\n\n<li><code>include_vars<\/code><\/li>\n\n\n<li><code>set_fact<\/code> and registered vars<\/li>\n\n\n<li>role and <code>include_role<\/code> params<\/li>\n\n\n<li>include params<\/li>\n\n\n<li>extra vars, <code>-e \"user=my_user\"<\/code> (always win)<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">How Ansible Tower \/ AWX fits into this order<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Tower (now Red Hat Ansible Automation Platform) and AWX run <code>ansible-playbook<\/code> for you, so all of the levels above still apply. On top of them they add their own sources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Inventory, group and host variables<\/strong> 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.<\/li>\n\n\n<li><strong>Job template extra variables<\/strong>, <strong>survey answers<\/strong> and <strong>prompt-on-launch values<\/strong> are passed to Ansible as extra vars, so they override everything defined in playbooks, roles and inventories.<\/li>\n\n\n<li>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 <strong>workflow<\/strong> override those of the job templates inside it.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is why a value typed into a survey wins even if the playbook sets the same variable in <code>vars:<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example: see precedence in action<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># inventory<\/span>\n&#91;webservers]\nweb1 ansible_host=<span class=\"hljs-number\">192.168<\/span><span class=\"hljs-number\">.1<\/span><span class=\"hljs-number\">.10<\/span>\n\n<span class=\"hljs-comment\"># roles\/web\/defaults\/main.yml<\/span>\nhttp_port: <span class=\"hljs-number\">80<\/span>\n\n<span class=\"hljs-comment\"># group_vars\/webservers.yml<\/span>\nhttp_port: <span class=\"hljs-number\">8080<\/span>\n\n<span class=\"hljs-comment\"># site.yml<\/span>\n- hosts: webservers\n  roles:\n    - web\n  tasks:\n    - name: Show which value won\n      ansible.builtin.debug:\n        <span class=\"hljs-keyword\">var<\/span>: http_port<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$ ansible-playbook -i inventory site.yml\nok: &#91;web1] =&gt; { <span class=\"hljs-string\">\"http_port\"<\/span>: <span class=\"hljs-number\">8080<\/span> }     <span class=\"hljs-comment\"># group_vars beats role defaults<\/span>\n\n$ ansible-playbook -i inventory site.yml -e http_port=<span class=\"hljs-number\">9090<\/span>\nok: &#91;web1] =&gt; { <span class=\"hljs-string\">\"http_port\"<\/span>: <span class=\"hljs-string\">\"9090\"<\/span> }   <span class=\"hljs-comment\"># extra vars beat everything<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Note that <code>-e key=value<\/code> passes values as strings. Pass JSON, for example <code>-e '{\"http_port\": 9090}'<\/code>, if you need a number, list or boolean.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to find out which value Ansible is using<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Print the variable in a task: <code>ansible.builtin.debug: var=http_port<\/code>.<\/li>\n\n\n<li>Check a single host without a playbook: <code>ansible -i inventory web1 -m ansible.builtin.debug -a \"var=http_port\"<\/code>.<\/li>\n\n\n<li>See all inventory variables for a host: <code>ansible-inventory -i inventory --host web1<\/code> (shows inventory sources only, not play or role vars).<\/li>\n\n\n<li>Run with <code>-vvv<\/code> to see which files Ansible loads.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Common mistakes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Putting overridable values in <code>roles\/x\/vars\/main.yml<\/code>.<\/strong> Role vars (level 15) beat inventory <code>group_vars<\/code> and <code>host_vars<\/code>, so users cannot override them from the inventory. Put values you expect people to change in <code>defaults\/main.yml<\/code>.<\/li>\n\n\n<li><strong>Expecting group_vars to beat host_vars.<\/strong> Host variables always override group variables.<\/li>\n\n\n<li><strong>Two groups at the same level setting the same variable.<\/strong> Ansible merges sibling groups alphabetically, so the last group name wins. Set <code>ansible_group_priority<\/code> on a group to control this.<\/li>\n\n\n<li><strong>Expecting dictionaries to merge.<\/strong> By default a dictionary defined at a higher level replaces the whole dictionary, it does not merge keys.<\/li>\n\n\n<li><strong>Trying to override extra vars with set_fact.<\/strong> Nothing overrides extra vars, not even <code>set_fact<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Rules of thumb<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Role defaults for values users may change; role vars for internal constants.<\/li>\n\n\n<li>Inventory <code>group_vars<\/code> for environment differences (dev, staging, prod).<\/li>\n\n\n<li>Prefix role variables with the role name (<code>web_http_port<\/code>) to avoid accidental clashes.<\/li>\n\n\n<li>Use <code>-e<\/code> or Tower surveys only for deliberate one-off overrides.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Which variable has the highest precedence in Ansible?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Extra vars passed with <code>-e<\/code> (or from Tower job templates, surveys and launch prompts). They override every other source.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Which has the lowest precedence?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Role defaults. Command line options such as <code>-u<\/code> are listed below them, but they are connection settings rather than variables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I change the precedence order?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. You can only control which group wins between groups at the same level with <code>ansible_group_priority<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do Tower survey answers override playbook vars?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Survey answers are passed as extra vars, so they override <code>vars:<\/code> in the playbook, role vars and inventory variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/ansible-all-ways-to-define-variables-in-ansible-inventory-hosts\/\">Ansible: All Ways to Define Variables in Inventory\/Hosts<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/example-code-of-ansible-variable-with-playbook\/\">Ansible Variables in Playbook<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/how-can-i-pass-variable-to-ansible-playbook-in-the-command-line\/\">How to pass variables to an Ansible playbook on the command line<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/infrastructure-as-code\/ansible\/\">All Ansible tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5038],"tags":[26228,5044],"class_list":["post-5300","post","type-post","status-publish","format-standard","hentry","category-ansible","tag-ansible-playbooks","tag-ansible-tower"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5300"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5300\/revisions"}],"predecessor-version":[{"id":79987,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5300\/revisions\/79987"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}