{"id":17096,"date":"2022-01-31T20:50:34","date_gmt":"2022-01-31T19:50:34","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/"},"modified":"2024-09-11T15:05:55","modified_gmt":"2024-09-11T13:05:55","slug":"what-is-variable-precedence-in-ansible","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/","title":{"rendered":"What is Variable Precedence in Ansible?"},"content":{"rendered":"<p>In Ansible, definition of variable precedence is often overlooked. Despite what I wrote in my previous blog (<a href=\"https:\/\/www.dbi-services.com\/blog\/use-ansible-like-a-programming-language\/\" rel=\"noopener\" target=\"_blank\">Use Ansible like a Programming Language<\/a>), Ansible is not exactly a programming language. There is a difference, especially on the way variables are managed.<br \/>\nAt the beginning, as I am used to scripting, I was a bit confused by the way Ansible is handling variables.<br \/>\nFor example, let\u2019s say you have a string variable defined in group_vars\/all.yml:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">---\nMyVar: \u2018all.yml\u2019<\/pre>\n<p>In same file, I define another variable using that variable:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 3\">WhereIsMyVariableEvaluated: \u2018MyVar is coming from {{ MyVar }}\u2019<\/pre>\n<p>I initially expected <em>WhereIsMyVariableEvaluated <\/em>variable to always be equal to \u2018<em>MyVar is coming from all.yml<\/em>\u2019 if all steps are evaluated in sequence. However, it might not depending on <em>MyVar<\/em> value override. This is variable precedence.<br \/>\nThus, Ansible documentation has is a nice long list of priorities (from low to high priority):<\/p>\n<ol>\n<li>command line values (for example, -u my_user, these are not variables)<\/li>\n<li>role defaults (defined in role\/defaults\/main.yml)<\/li>\n<li>inventory file or script group vars<\/li>\n<li>inventory group_vars\/all<\/li>\n<li>playbook group_vars\/all<\/li>\n<li>inventory group_vars\/*<\/li>\n<li>playbook group_vars\/*<\/li>\n<li>inventory file or script host vars<\/li>\n<li>inventory host_vars\/*<\/li>\n<li>playbook host_vars\/*<\/li>\n<li>host facts \/ cached set_facts<\/li>\n<li>play vars<\/li>\n<li>play vars_prompt<\/li>\n<li>play vars_files<\/li>\n<li>role vars (defined in role\/vars\/main.yml)<\/li>\n<li>block vars (only for tasks in block)<\/li>\n<li>task vars (only for the task)<\/li>\n<li>include_vars<\/li>\n<li>set_facts \/ registered vars<\/li>\n<li>role (and include_role) params<\/li>\n<li>include params<\/li>\n<li>extra vars (for example, -e &#8220;user=my_user&#8221;)(always win precedence)<\/li>\n<\/ol>\n<p>OK, but then what does it mean?<\/p>\n<h3>Run a simple playbook<\/h3>\n<p>First, I create a playbook with this simple task that will display <em>WhereIsMyVariableEvaluated<\/em> variable content:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">---\n- name: Test playbook\n  hosts: localhost\n  tasks:\n    - name: Print WhereIsMyVariableEvaluated\n      ansible.builtin.debug:\n        msg: '{{ WhereIsMyVariableEvaluated }}'\n<\/pre>\n<p>Secondly, I have setup a WSL (Windows Subsystem Linux) Ubuntu machine with Ansible package installed and to run it, as it does not imply any server modification, I am using &#8220;<em>-c local<\/em>&#8221; option.<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1; highlight: 8\">$ ansible-playbook playbook.yml -c local\n\nPLAY [Test playbook] **********************************************************************************\n\nTASK [Print WhereIsMyVariableEvaluated] ***************************************************************\nok: [localhost] =&gt; {\n    \"changed\": false,\n    \"msg\": \"MyVar is coming from all.yml\"\n}\n\nPLAY RECAP ********************************************************************************************\nlocalhost          : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0<\/pre>\n<p>Until now, nothing unexpected.<br \/>\nAfterward, we can try to define <em>MyVar<\/em> in host_vars\/localhost.yml (priority 10):<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1; highlight: 6\">PLAY [Test playbook] **********************************************************************************\n\nTASK [Print WhereIsMyVariableEvaluated] ***************************************************************\nok: [localhost] =&gt; {\n    \"changed\": false,\n    \"msg\": \"MyVar is coming from localhost.yml\"\n}<\/pre>\n<p>Next, defining <em>MyVar<\/em> from within playbook itself, adding lines 4 and 5:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">---\n- name: Test playbook\n  hosts: localhost\n  vars:\n    MyVar: 'playbook'\n  tasks:\n    - name: Print WhereIsMyVariableEvaluated\n      ansible.builtin.debug:\n        msg: '{{ WhereIsMyVariableEvaluated }}'\n<\/pre>\n<p>As a result, output of playbook run:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1; highlight: 5\">...\nTASK [Print WhereIsMyVariableEvaluated] ********************************************************************************\nok: [localhost] =&gt; {\n    \"changed\": false,\n    \"msg\": \"MyVar is coming from playbook\"\n}\n...<\/pre>\n<p>Finally, I can run playbook with an argument (highest priority (22)):<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1; highlight: 6\">$ ansible-playbook playbook.yml -c local -e \"MyVar=CLI\"\n...\nTASK [Print WhereIsMyVariableEvaluated] ********************************************************************************\nok: [localhost] =&gt; {\n    \"changed\": false,\n    \"msg\": \"MyVar is coming from CLI\"\n}\n...<\/pre>\n<p>All these modifications were done by adding only, meaning nothing was removed from any yml file where MyVar was defined. This means previous definitions remained, but still highest priority is chosen by Ansible.<\/p>\n<h3>Conclusion<\/h3>\n<p>In short, WhereIsMyVariableEvaluated uses MyVar which was defined more recently. This mechanism allows to define default values for variable and overload them when required for specific hosts  or groups while running a playbook.<br \/>\nI hope this have helped you to better understand and feel how Ansible variable precedence works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Ansible, definition of variable precedence is often overlooked. Despite what I wrote in my previous blog (Use Ansible like a Programming Language), Ansible is not exactly a programming language. There is a difference, especially on the way variables are managed. At the beginning, as I am used to scripting, I was a bit confused [&hellip;]<\/p>\n","protected":false},"author":109,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1321,1320],"tags":[150],"type_dbi":[],"class_list":["post-17096","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","tag-ansible"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>What is Variable Precedence in Ansible? - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Variable Precedence in Ansible?\" \/>\n<meta property=\"og:description\" content=\"In Ansible, definition of variable precedence is often overlooked. Despite what I wrote in my previous blog (Use Ansible like a Programming Language), Ansible is not exactly a programming language. There is a difference, especially on the way variables are managed. At the beginning, as I am used to scripting, I was a bit confused [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-31T19:50:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-11T13:05:55+00:00\" \/>\n<meta name=\"author\" content=\"DevOps\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DevOps\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/\"},\"author\":{\"name\":\"DevOps\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735\"},\"headline\":\"What is Variable Precedence in Ansible?\",\"datePublished\":\"2022-01-31T19:50:34+00:00\",\"dateModified\":\"2024-09-11T13:05:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/\"},\"wordCount\":442,\"commentCount\":0,\"keywords\":[\"Ansible\"],\"articleSection\":[\"Ansible\",\"DevOps\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/\",\"name\":\"What is Variable Precedence in Ansible? - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2022-01-31T19:50:34+00:00\",\"dateModified\":\"2024-09-11T13:05:55+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is Variable Precedence in Ansible?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735\",\"name\":\"DevOps\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g\",\"caption\":\"DevOps\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/devops\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What is Variable Precedence in Ansible? - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/","og_locale":"en_US","og_type":"article","og_title":"What is Variable Precedence in Ansible?","og_description":"In Ansible, definition of variable precedence is often overlooked. Despite what I wrote in my previous blog (Use Ansible like a Programming Language), Ansible is not exactly a programming language. There is a difference, especially on the way variables are managed. At the beginning, as I am used to scripting, I was a bit confused [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/","og_site_name":"dbi Blog","article_published_time":"2022-01-31T19:50:34+00:00","article_modified_time":"2024-09-11T13:05:55+00:00","author":"DevOps","twitter_card":"summary_large_image","twitter_misc":{"Written by":"DevOps","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/"},"author":{"name":"DevOps","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735"},"headline":"What is Variable Precedence in Ansible?","datePublished":"2022-01-31T19:50:34+00:00","dateModified":"2024-09-11T13:05:55+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/"},"wordCount":442,"commentCount":0,"keywords":["Ansible"],"articleSection":["Ansible","DevOps"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/","url":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/","name":"What is Variable Precedence in Ansible? - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2022-01-31T19:50:34+00:00","dateModified":"2024-09-11T13:05:55+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/what-is-variable-precedence-in-ansible\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What is Variable Precedence in Ansible?"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4cd1b5f8a3de93f05a16ab8d7d2b7735","name":"DevOps","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cdd2dd7441774355062c0f0f68612296b059cd1e2ff6c7af0b15dba0ed64a85f?s=96&d=mm&r=g","caption":"DevOps"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/devops\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/17096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/109"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=17096"}],"version-history":[{"count":1,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/17096\/revisions"}],"predecessor-version":[{"id":34728,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/17096\/revisions\/34728"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=17096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=17096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=17096"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=17096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}