๐”๐ง๐๐ž๐ซ๐ฌ๐ญ๐š๐ง๐๐ข๐ง๐  ๐€๐ง๐ฌ๐ข๐›๐ฅ๐ž ๐ˆ๐ง๐ฏ๐ž๐ง๐ญ๐จ๐ซ๐ฒ: ๐Œ๐š๐ง๐š๐ ๐ข๐ง๐  ๐š๐ง๐ ๐Ž๐ซ๐ ๐š๐ง๐ข๐ณ๐ข๐ง๐  ๐˜๐จ๐ฎ๐ซ ๐ˆ๐ง๐Ÿ๐ซ๐š๐ฌ๐ญ๐ซ๐ฎ๐œ๐ญ๐ฎ๐ซ๐ž

Athira KK
8 min readDec 10, 2023

--

Hey Techiesโ€ฆ๐Ÿ‘‹

In this blog, weโ€™re going to discuss about ๐€๐ง๐ฌ๐ข๐›๐ฅ๐ž ๐ˆ๐ง๐ฏ๐ž๐ง๐ญ๐จ๐ซ๐ฒ: ๐Œ๐š๐ง๐š๐ ๐ข๐ง๐  ๐š๐ง๐ ๐Ž๐ซ๐ ๐š๐ง๐ข๐ณ๐ข๐ง๐  ๐˜๐จ๐ฎ๐ซ ๐ˆ๐ง๐Ÿ๐ซ๐š๐ฌ๐ญ๐ซ๐ฎ๐œ๐ญ๐ฎ๐ซ๐ž.

๐—ช๐—ต๐—ฎ๐˜ ๐—ถ๐˜€ ๐—”๐—ป๐˜€๐—ถ๐—ฏ๐—น๐—ฒ ๐—œ๐—ป๐˜ƒ๐—ฒ๐—ป๐˜๐—ผ๐—ฟ๐˜†?
Ansible Inventory is the backbone of server management, housing crucial details about your servers. Traditionally, it was a static file, but Ansibleโ€™s true prowess shines with dynamic inventories.

๐—œ๐—ป๐˜๐—ฟ๐—ผ๐—ฑ๐˜‚๐—ฐ๐—ถ๐—ป๐—ด ๐——๐˜†๐—ป๐—ฎ๐—บ๐—ถ๐—ฐ ๐—œ๐—ป๐˜ƒ๐—ฒ๐—ป๐˜๐—ผ๐—ฟ๐˜†:
Dynamic Inventory takes server management to the next level by enabling real-time updates. No more static files! Instead, it fetches server information on-the-fly from external sources like cloud providers or databases.

๐—ช๐—ต๐˜† ๐——๐˜†๐—ป๐—ฎ๐—บ๐—ถ๐—ฐ ๐—œ๐—ป๐˜ƒ๐—ฒ๐—ป๐˜๐—ผ๐—ฟ๐˜†?
โœ”Real-Time Updates: Reflect changes instantly.
โœ”Adaptability: Ideal for dynamic environments.
โœ”Grouping Magic: Efficiently manage server categories.
โœ”Structured Hierarchy: Parent-child relationships bring order.

๐—Ÿ๐—ถ๐—ป๐—ธ๐—ถ๐—ป๐—ด ๐—ฅ๐—ฒ๐—ฎ๐—น๐—ถ๐˜๐˜† ๐˜๐—ผ ๐—”๐—ป๐˜€๐—ถ๐—ฏ๐—น๐—ฒ:
Dynamic Inventory seamlessly integrates with your environment. If youโ€™re on AWS, Azure, or any major cloud platform, Ansible can automatically fetch server details. Changes in your infrastructure? No worries โ€” Ansible stays in sync.

๐—˜๐—ณ๐—ณ๐—ถ๐—ฐ๐—ถ๐—ฒ๐—ป๐˜ ๐—ฆ๐—ฒ๐—ฟ๐˜ƒ๐—ฒ๐—ฟ ๐—š๐—ฟ๐—ผ๐˜‚๐—ฝ๐—ถ๐—ป๐—ด:
With Ansible Inventory, youโ€™re not stuck managing servers individually. Leverage grouping to categorize servers based on roles, locations, or any criteria. Need to update web servers? Simply target the โ€œweb_serversโ€ group โ€” itโ€™s that streamlined!

๐—œ๐—ก๐—œ ๐˜ƒ๐˜€. ๐—ฌ๐—”๐— ๐—Ÿ:
Choose your syntax โ€” Ansible supports both INI and YAML formats for inventory. INI is straightforward, while YAML adds flexibility and structure, catering to the scale and complexity of your IT environment.

In the realm of server management, choosing an inventory format is like selecting an organizational chart that seamlessly aligns with your companyโ€™s structure and scale.

Consider two scenarios that reflect the diversity of organizational needs:

๐ŸŒฑ ๐—ฆ๐—ฐ๐—ฒ๐—ป๐—ฎ๐—ฟ๐—ถ๐—ผ 1: ๐—ฆ๐—บ๐—ฎ๐—น๐—น ๐—ฆ๐˜๐—ฎ๐—ฟ๐˜-๐˜‚๐—ฝ

In this scenario, a small start-up operates with a handful of services managing basic functions such as web hosting and database management. The ideal choice here is a simple INI format inventory, mirroring a basic organizational chart with just a few departments.

๐ŸŒ ๐—ฆ๐—ฐ๐—ฒ๐—ป๐—ฎ๐—ฟ๐—ถ๐—ผ 2: ๐— ๐˜‚๐—น๐˜๐—ถ๐—ป๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—–๐—ผ๐—ฟ๐—ฝ๐—ผ๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป

For a multinational corporation with hundreds of servers globally, each handling diverse functions like e-commerce, customer support, and data analysis, a more detailed and structured inventory is paramount. Here, the YAML format comes into play, resembling a complex organizational chart with various departments, sub-departments, and teams.

๐Ÿค ๐—™๐—น๐—ฒ๐˜…๐—ถ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜† ๐—ถ๐—ป ๐—š๐—ฟ๐—ผ๐˜‚๐—ฝ๐—ถ๐—ป๐—ด:

The flexibility of these inventory formats empowers you to group servers based on roles (e.g., web servers, database servers), geographic locations (e.g., U.S., Europe, Asia), or any criteria crucial to your organizationโ€™s dynamics.

๐Ÿ” ๐— ๐—ผ๐˜€๐˜ ๐—จ๐˜€๐—ถ๐—ป๐—ด ๐—”๐—ป๐˜€๐—ถ๐—ฏ๐—น๐—ฒโ€™๐˜€ ๐—œ๐—ป๐˜ƒ๐—ฒ๐—ป๐˜๐—ผ๐—ฟ๐˜† ๐—™๐—ผ๐—ฟ๐—บ๐—ฎ๐˜๐˜€:
โœ”INI Format
โœ”YAML Format

๐Ÿ”„ ๐—–๐—ต๐—ผ๐—ผ๐˜€๐—ถ๐—ป๐—ด ๐—ฌ๐—ผ๐˜‚๐—ฟ ๐—ข๐—ฟ๐—ด๐—ฎ๐—ป๐—ถ๐˜‡๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—•๐—น๐˜‚๐—ฒ๐—ฝ๐—ฟ๐—ถ๐—ป๐˜:

Just as a company selects the right organizational chart, your project should choose the inventory format that aligns seamlessly with its needs. Whether itโ€™s the simplicity of INI or the flexibility of YAML, the key is to ensure effective collaboration between your servers and departments to achieve organizational objectives.

Letโ€™s see how the INI and YAML formats play a crucial role in defining and organizing your infrastructure, providing with the flexibility and clarity needed for effective Ansible playbook execution.

Inventory Formats

We can create inventory file in one of many formats, depending on the inventory plugins we have. The most common formats are INI and YAML.

INI Format

INI file based inventory, sections are groups or group related with Entries in sections [group_1] are hosts, members of the group. Hosts can have variables defined inline as key/value pairs The children modifier indicates that the section contains groups. The vars modifier indicates that the section contains variables special :modifiers. separated by =. assigned to members of the group.

โœ”Classic and simple.

โœ”Suitable for basic inventories.

Sections: Denoted by square brackets ([]), allowing grouping of hosts based on functionality.(e.g., [web_servers]).

Hosts: Listed under sections with unique names (server1, server2). Variables: Assigned to hosts using key=value pairs, enhancing flexibility and customization.

YAML Format:

YAML-based inventory, should start with the all group and contain hosts/vars/children entries. Host entries can have sub-entries defined, which will be treated as variables.Vars entries are normal group vars. Children are โ€˜child groupsโ€™ , which can also have their own File MUST have a valid extension, defined in configuration.

โœ”Offers a more structured and readable approach.

โœ”Suitable for complex environments with nested variables.

Structure: YAML uses indentation to represent the hierarchy, making it visually intuitive.

Hosts: Defined under named groups (web_servers, db_servers) with associated hosts and their attributes.

Variables: Assigned as key-value pairs within each hostโ€™s details.

Letโ€™s consider a practical scenario to know more about Ansible Inventory.

****************************************************************************

Prerequisites โ€” 3 Virtual Machines

1. server โ€” 1 CPU โ€” 1GB RAM (Python 2.7) โ€” Ansible Server

2. node1โ€“1 CPU โ€” 1GB RAM ( python 2.6 and above) โ€” Ansible Client

3. node2โ€“1 CPU โ€” 1GB RAM ( python 2.6 and above) โ€” Ansible Client

From ansible user execute below command:

ansible all -m ping

the above ping command should return with a ping / pong green color.

***************************************************************************

Certainly! The ansible.cfg file is the configuration file for Ansible, a popular open-source automation tool. It allows you to configure various settings for Ansible, such as default inventory locations, remote user, and more. Below is a simple example of an ansible.cfg file:

This example includes some common configurations:

  • inventory: Specifies the path to your inventory file, which contains information about your managed hosts.
  • remote_user: Specifies the default user that Ansible should use when connecting to remote hosts.

You can customize these settings based on your specific needs and environment. Ensure that you have the necessary permissions and security considerations in place, especially when dealing with sensitive information such as passwords.

Feel free to adjust the values according to your requirements. Additionally, you can refer to the official Ansible documentation for more details on configuration options.

Below are the contents of an Ansible inventory file (hosts). In your example, you have two groups: [prod] and [backup], each containing a single host (node1 and node2, respectively).

Then, youโ€™ve used the ansible command with the โ€” list option to display the hosts in different groups. Hereโ€™s a breakdown:

  • ansible all โ€” list: Lists all hosts, which includes both node1 and node2.
  • ansible prod โ€” list: Lists hosts in the [prod] group, which is just node1.
  • ansible backup โ€” list: Lists hosts in the [backup] group, which is just node2.

Letโ€™s take the below scenario :

Deploy all plays across the servers listed in the inventory file, ensuring that only one play or task is executed exclusively on the servers within the โ€œprodโ€ group.

Certainly! To achieve this, you can create an Ansible playbook that includes multiple plays, with one of the plays specifically targeting the hosts in the [prod] group. Below is an example playbook that you can use:

Syntax:

---
- name: Install Plays
hosts: all
tasks:
- name: Your common tasks for all servers
# Add your common tasks here

- name: Install Plays for Prod
hosts: prod
tasks:
- name: Your specific tasks for the prod group
# Add your tasks specific to the prod group here

This Ansible playbook is designed to install and configure the Apache HTTP server (httpd) on all hosts. Additionally, it includes a task to restart the HTTPD service, but this task is conditionally executed only on hosts within the โ€œprodโ€ group.

Breaking it down:

  • Install httpd:
  • The first task, named โ€œInstall httpd,โ€ uses the yum module to install the latest version of the Apache HTTP server (httpd) on all hosts (hosts: all).
  • Restart httpd on prod servers:
  • The second task, named โ€œRestart httpd on prod servers,โ€ utilizes the service module to restart the HTTPD service.
  • However, the when condition (when: โ€œโ€˜prodโ€™ in group_namesโ€) ensures that this task is only executed on hosts that belong to the โ€œprodโ€ group. It checks if the string โ€œprodโ€ is present in the list of group names (group_names).

In summary, this playbook will install the Apache HTTP server on all hosts and restart the HTTPD service only on hosts that are part of the โ€œprodโ€ group. If you run this playbook using the ansible-playbook command, it will apply these tasks to the specified hosts in your inventory file.

---
- name: Install and Configure HTTPD
hosts: all
tasks:
- name: Install httpd
yum:
name: httpd
state: latest

- name: Restart httpd on prod servers
service:
name: httpd
state: restarted
when: "'prod' in group_names"

The below command will check the syntax of the playbook and report any syntax errors if they exist. If there are no syntax errors, it will display a message indicating that the syntax is okay.

ansible-playbook inventoryeg.yaml - syntax-check

Now, letโ€™s execute the playbook by running the following command

ansible-playbook inventoryeg.yaml

The output indicates that the playbook has been executed successfully. Hereโ€™s a breakdown of the relevant information:

  • PLAY [Install and Configure HTTPD]: This is the name of the play in your playbook.
  • TASK [Gathering Facts]: Ansible starts by gathering facts about the target hosts. This includes information about the system such as operating system details, network interfaces, and more. Itโ€™s a necessary step for many Ansible modules.
  • TASK [Install httpd]: This task uses the yum module to install the latest version of the Apache HTTP server (httpd). Both node1 and node2 report as โ€œok,โ€ indicating that the task was executed successfully on both hosts.
  • TASK [Restart httpd on prod servers]: This task restarts the HTTPD service, but it is conditionally executed only on hosts in the โ€œprodโ€ group. In this case, node2 is skipped because it is not in the โ€œprodโ€ group, and node1 reports as โ€œchanged,โ€ indicating that the task was executed and resulted in a change (the service was restarted).
  • PLAY RECAP: This section provides a summary of the playโ€™s results for each host. For node1, it shows that 3 tasks were okay (ok=3), 1 task resulted in a change (changed=1), and there were no failures (failed=0). For node2, it shows 2 tasks were okay (ok=2), 0 changes, and 1 task was skipped (skipped=1). The โ€œchangedโ€ count indicates that a task modified the system state.

I hope that this blog has provided you with valuable insights into the practical and beneficial aspects of Ansible inventories.

๐Ÿ’นNext day โ€” Demystifying Ansible Variables: The Key to Flexible and Dynamic Automation.

โญโญโญ Enjoy your learningโ€ฆ.!!! โญโญโญ

--

--

Athira KK
Athira KK

Written by Athira KK

AWS DevOps Engineer | Calico Big Cats Ambassador | WomenTech Global Ambassador | LinkedIn Top Cloud Computing Voice

No responses yet