{"id":2462,"date":"2023-02-08T15:06:37","date_gmt":"2023-02-08T15:06:37","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2462"},"modified":"2023-02-08T15:30:29","modified_gmt":"2023-02-08T15:30:29","slug":"ansible-variables","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/02\/08\/ansible-variables\/","title":{"rendered":"Ansible: Variables"},"content":{"rendered":"\n<p>Ansible Variables are used to store values that can be reused throughout an Ansible playbook. They can be set in a number of ways, including being passed as command-line options, defined in an inventory file, or defined in a playbook itself. Variables can be used to store values such as file paths, user-defined parameters, and results of tasks.<\/p>\n\n\n\n<p>Ansible System Variables, on the other hand, are predefined variables provided by Ansible. These variables contain information about the host, such as the host name, operating system, IP address, etc. These variables are automatically available in playbooks and can be accessed using the &#8220;hostvars&#8221; dictionary. For example, the hostname of a target host can be accessed using the variable &#8220;ansible_hostname&#8221;.<\/p>\n\n\n\n<p>System Variables can be useful in writing more dynamic playbooks that adapt to the target host&#8217;s characteristics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code has-black-color has-pale-cyan-blue-background-color has-text-color has-background has-small-font-size\"><code>---\n- name: Example Playbook with Variables and System Variables\n  hosts: all\n  gather_facts: true\n  tasks:\n    - name: Print IP and FQDN\n      debug:\n        msg: \"The IP address is {{ ansible_default_ipv4.address }} and the FQDN is {{ ansible_fqdn }}\"\n    - name: Print a custom variable\n      debug:\n        msg: \"The custom message is: {{ custom_message }}\"\n  vars:\n    custom_message: \"Hello from the playbook!\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">System Variables<\/h2>\n\n\n\n<p>Ansible system variables are special variables that are used to represent information about the system, such as the hostname, architecture, IP address, and others. These variables are set by the Ansible control node and are available to all playbooks, tasks, and roles. Some of the common system variables in Ansible include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ansible_hostname<\/code>: hostname of the system<\/li>\n\n\n\n<li><code>ansible_os_family<\/code>: family of the operating system (e.g. RedHat, Debian, etc.)<\/li>\n\n\n\n<li><code>ansible_distribution<\/code>: distribution of the operating system (e.g. CentOS, Ubuntu, etc.)<\/li>\n\n\n\n<li><code>ansible_distribution_version<\/code>: version of the operating system distribution<\/li>\n\n\n\n<li><code>ansible_architecture<\/code>: architecture of the system (e.g. x86_64, aarch64, etc.)<\/li>\n\n\n\n<li><code>ansible_all_ipv4_addresses<\/code>: list of all IPv4 addresses of the system<\/li>\n\n\n\n<li><code>ansible_default_ipv4<\/code>: default IPv4 address of the system<\/li>\n<\/ul>\n\n\n\n<p>These variables can be used in tasks and playbooks to conditionally execute tasks based on system information, customize the behavior of tasks and playbooks, and more.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-pale-cyan-blue-background-color has-text-color has-background has-small-font-size\"><code>- name: Get IP and FQDN of Host\n  hosts: all\n  gather_facts: yes\n  tasks:\n    - name: Debug print host IP\n      debug:\n        msg: \"Host IP: {{ ansible_default_ipv4.address }}\"\n\n    - name: Debug print host FQDN\n      debug:\n        msg: \"Host FQDN: {{ ansible_fqdn }}\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ansible Variables are used to store values that can be reused throughout an Ansible playbook. They can be set in a number of ways, including being passed as command-line options, defined in an inventory file, or defined in a playbook itself. Variables can be used to store values such as file paths, user-defined parameters, and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2463,"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,280],"class_list":["post-2462","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-ansible","tag-variables"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2462","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2462"}],"version-history":[{"count":2,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2462\/revisions"}],"predecessor-version":[{"id":2465,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2462\/revisions\/2465"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2463"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}