{"id":2537,"date":"2023-03-15T15:51:47","date_gmt":"2023-03-15T15:51:47","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2537"},"modified":"2023-03-15T15:51:49","modified_gmt":"2023-03-15T15:51:49","slug":"ansible-variables-2","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/03\/15\/ansible-variables-2\/","title":{"rendered":"Ansible Variables"},"content":{"rendered":"\n<div class=\"wp-block-cover\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim\"><\/span><img decoding=\"async\" width=\"359\" height=\"105\" class=\"wp-block-cover__image-background wp-image-2539\" alt=\"\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/03\/ans-vars.png\" data-object-fit=\"cover\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/03\/ans-vars.png 359w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/03\/ans-vars-300x88.png 300w\" sizes=\"(max-width: 359px) 100vw, 359px\" \/><div class=\"wp-block-cover__inner-container is-layout-flow wp-block-cover-is-layout-flow\">\n<p class=\"has-text-align-center has-large-font-size\">Ansible Variables<\/p>\n<\/div><\/div>\n\n\n\n<p>Variables are a crucial part of <code>Ansible<\/code> and are used to store values that can be used across <code>playbooks<\/code>, <code>roles<\/code>, and <code>tasks<\/code>. Understanding how variables work and their precedence is crucial to writing efficient and reliable playbooks.<\/p>\n\n\n\n<p><code>Ansible<\/code> provides a number of ways to define and use variables. Some of the common ways are:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>Inline variables<\/code>: Variables can be defined inline using the <code>vars<\/code> keyword in the task definition.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>- name: Example task\n  debug:\n    var: my_var\n  vars:\n    my_var: \"my value\"<\/code><\/pre>\n\n\n\n<p>2. <code>Playbook variables<\/code>: Variables can be defined at the playbook level using the <code>vars<\/code> keyword<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>- name: Example playbook\n  hosts: all\n  vars:\n    my_var: \"my value\"\n  tasks:\n    - name: Example task\n      debug:\n        var: my_var<\/code><\/pre>\n\n\n\n<p>3. <code>Role variables<\/code>: Variables can be defined at the role level using the <code>vars<\/code> directory in the role directory structure.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_role\/\n\u251c\u2500\u2500 tasks\/\n\u2514\u2500\u2500 vars\/\n    \u2514\u2500\u2500 main.yml<\/code><\/pre>\n\n\n\n<p>The variables defined in this directory can be accessed using the <code>vars<\/code> keyword.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>- name: Example role\n  hosts: all\n  roles:\n    - my_role\n  tasks:\n    - name: Example task\n      debug:\n        var: my_role_var<\/code><\/pre>\n\n\n\n<p>4. <code>Inventory variables<\/code>: Variables can be defined in the inventory file using the <code>host_vars<\/code> and <code>group_vars<\/code> directories.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>inventory\/\n\u251c\u2500\u2500 host_vars\/\n\u2502   \u2514\u2500\u2500 my_host.yml\n\u251c\u2500\u2500 group_vars\/\n\u2502   \u2514\u2500\u2500 my_group.yml\n\u2514\u2500\u2500 hosts<\/code><\/pre>\n\n\n\n<p>The variables defined in these directories can be accessed using the <code>hostvars<\/code> and <code>group_vars<\/code> keywords respectively.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>- name: Example playbook\n  hosts: all\n  tasks:\n    - name: Example task\n      debug:\n        var: hostvars&#91;'my_host']&#91;'my_var']<\/code><\/pre>\n\n\n\n<p>The order of precedence of variables in <code>Ansible<\/code> is as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Role variables<\/li>\n\n\n\n<li>Playbook variables<\/li>\n\n\n\n<li>Inventory variables<\/li>\n\n\n\n<li>Registered variables<\/li>\n\n\n\n<li>Set facts<\/li>\n\n\n\n<li>Extra variables (<code>--extra-vars<\/code> or <code>-e<\/code> command line argument)<\/li>\n<\/ol>\n\n\n\n<p>It is important to keep this order in mind when defining variables to ensure that the correct value is used. For example, if a variable is defined in both the playbook and role variables, the role variable will take precedence.<\/p>\n\n\n\n<p>In addition to the order of precedence, Ansible also provides the ability to override variables using tags. Tags are used to selectively execute tasks based on the tags defined in the playbook. By default, all tasks are executed unless a tag is specified. However, tags can be used to specify which tasks to execute.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><code>- name: Example playbook\n  hosts: all\n  tasks:\n    - name: Example task\n      debug:\n        var: my_var\n      tags:\n        - my_tag\n<\/code><\/code><\/pre>\n\n\n\n<p>In this example, the task will only be executed if the <code>my_tag<\/code> tag is specified when running the playbook.<\/p>\n\n\n\n<p>In summary, variables are a powerful feature of <code>Ansible<\/code> and are used to store values that can be used across playbooks, roles, and tasks. Understanding the order of precedence and how to use tags to override variables is crucial to writing efficient and reliable playbooks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variables are a crucial part of Ansible and are used to store values that can be used across playbooks, roles, and tasks. Understanding how variables work and their precedence is crucial to writing efficient and reliable playbooks. Ansible provides a number of ways to define and use variables. Some of the common ways are: 2. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2539,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[278],"class_list":["post-2537","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-ansible"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2537"}],"version-history":[{"count":1,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2537\/revisions"}],"predecessor-version":[{"id":2540,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2537\/revisions\/2540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2539"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}