๐๐ง๐๐๐ซ๐ฌ๐ญ๐๐ง๐๐ข๐ง๐ ๐๐ง๐ฌ๐ข๐๐ฅ๐ ๐๐ง๐ฏ๐๐ง๐ญ๐จ๐ซ๐ฒ: ๐๐๐ง๐๐ ๐ข๐ง๐ ๐๐ง๐ ๐๐ซ๐ ๐๐ง๐ข๐ณ๐ข๐ง๐ ๐๐จ๐ฎ๐ซ ๐๐ง๐๐ซ๐๐ฌ๐ญ๐ซ๐ฎ๐๐ญ๐ฎ๐ซ๐
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โฆ.!!! โญโญโญ