{"id":2452,"date":"2023-02-08T14:48:21","date_gmt":"2023-02-08T14:48:21","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2452"},"modified":"2023-02-08T15:31:01","modified_gmt":"2023-02-08T15:31:01","slug":"ansible-building-inventory","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/02\/08\/ansible-building-inventory\/","title":{"rendered":"Ansible: Building Inventory"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"663\" height=\"322\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/02\/an-inventory.png\" alt=\"\" class=\"wp-image-2454\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/02\/an-inventory.png 663w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/02\/an-inventory-300x146.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/02\/an-inventory-300x146@2x.png 600w\" sizes=\"(max-width: 663px) 100vw, 663px\" \/><\/figure>\n<\/div>\n\n\n<figure class=\"wp-block-pullquote has-white-color has-black-background-color has-text-color has-background has-small-font-size\" style=\"border-width:13px;border-radius:29px;font-style:normal;font-weight:400\"><blockquote><p>The blog is adapted from the information available <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/getting_started\/get_started_inventory.html#get-started-inventory\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p><cite>https:\/\/docs.ansible.com\/ansible\/<\/cite><\/blockquote><\/figure>\n\n\n\n<p>An Ansible inventory is a file that defines the hosts and groups of hosts that Ansible will manage. Inventory files are used to define the nodes (hosts) that will be managed by Ansible. These files can be in different formats, including INI and YAML.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote has-black-color has-pale-cyan-blue-background-color has-text-color has-background has-medium-font-size\" style=\"border-width:13px;border-radius:50px\"><blockquote><p>In <code>Ansible<\/code>, inventory refers to a file containing a list of hosts and their details (such as IP addresses, SSH credentials, etc.) that <code>Ansible<\/code> can interact with. Inventory variables are variables that are assigned to a specific host or group of hosts in the inventory file. These variables can be used to customize the behavior of <code>Ansible<\/code> playbooks and roles.<\/p><cite>ANSIBLE.com<\/cite><\/blockquote><\/figure>\n\n\n\n<p>The basic structure of an <code>Ansible<\/code> inventory file includes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Hosts<\/code>: defined by their IP addresses or hostnames.<\/li>\n\n\n\n<li><code>Groups<\/code>: a collection of hosts.<\/li>\n\n\n\n<li><code>Variables<\/code>: data that can be associated with a host or group and used in playbooks.<\/li>\n<\/ul>\n\n\n\n<p>To build an inventory, you need to decide on the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Hosts: List the IP addresses or hostnames of the nodes you want to manage.<\/li>\n\n\n\n<li>Groups: Create groups based on your organizational structure. For example, you could have a group for web servers, another for databases, and another for applications.<\/li>\n\n\n\n<li>Variables: Define variables for each host or group that you want to use in your playbooks.<\/li>\n<\/ol>\n\n\n\n<p>Here&#8217;s a simple example of an Ansible inventory in the INI format:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-luminous-vivid-amber-background-color has-text-color has-background has-small-font-size\"><code>&#91;webservers]\nweb1 ansible_host=192.168.1.10\nweb2 ansible_host=192.168.1.11\n\n&#91;dbservers]\ndb1 ansible_host=192.168.1.12\ndb2 ansible_host=192.168.1.13\n\n&#91;all:vars]\nansible_user=ansible<\/code><\/pre>\n\n\n\n<p>In this example, we have two groups: <code>webservers<\/code> and <code>dbservers<\/code>. Each group has two hosts defined by their IP addresses. Additionally, we have a <code>[all:vars]<\/code> section that defines a variable <code>ansible_user<\/code> for all hosts and groups.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Variables<\/h2>\n\n\n\n<p>Variables can be defined in an inventory file, either at the group or host level, and can be used in playbooks to configure tasks.<\/p>\n\n\n\n<p>Here&#8217;s an example of how variables can be defined in an inventory file:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-luminous-vivid-amber-background-color has-text-color has-background\"><code>&#91;web-server]\nhost1 ansible_host=192.168.1.100 http_port=80\nhost2 ansible_host=192.168.1.101 http_port=8080\n\n&#91;db-server]\nhost3 ansible_host=192.168.1.102 db_port=3306\nhost4 ansible_host=192.168.1.103 db_port=3306<\/code><\/pre>\n\n\n\n<p>In this example, the <code>web-server<\/code> and <code>db-server<\/code> groups have different variables defined for each host. These variables can then be referenced in a playbook to configure tasks specific to each group.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of YAML<\/h2>\n\n\n\n<pre class=\"wp-block-code has-black-color has-light-green-cyan-background-color has-text-color has-background\"><code>virtualmachines:\n  hosts:\n    vm01:\n      ansible_host: 192.0.2.50\n    vm02:\n      ansible_host: 192.0.2.51\n    vm03:\n      ansible_host: 192.0.2.52<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The blog is adapted from the information available here. https:\/\/docs.ansible.com\/ansible\/ An Ansible inventory is a file that defines the hosts and groups of hosts that Ansible will manage. Inventory files are used to define the nodes (hosts) that will be managed by Ansible. These files can be in different formats, including INI and YAML. In [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2454,"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,279],"class_list":["post-2452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-ansible","tag-inventory"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2452","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=2452"}],"version-history":[{"count":4,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2452\/revisions"}],"predecessor-version":[{"id":2461,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2452\/revisions\/2461"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2454"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}