Ansible Server Inventory: Manage Server Inventory with Ansible

Ansible Server Inventory: Manage Server Inventory with Ansible. In this post, we introduce Ansible inventory, types of inventory then shows you how to create a custom inventory to organize servers by group and name.

An inventory is an indispensable part of managing systems with Ansible, which is an open source automation tool. Ansible Inventory is a file that has data about the hosts and bunches of hosts that it impacts the automation process.

What is an Ansible Inventory?

In the case of Ansible, an inventory is an organized and cleanly formatted file that offers necessary data about the managed hosts. The main objective of the inventory is to clarify the target systems so that Ansible performs tasks and playbooks on them.

A well structured inventory comprises vital information such as hostnames, IP addresses, credentials, host groups, and SSH. The inventory works as a roadmap for Ansible to decide which systems to communicate with and how to manage the automation process.

An Ansible standard inventory file is written in INI or plain text format, even though YAML is getting more popular day by day for its readability and flexibility. Now how to store an inventory file? Well,  it is stored locally on the control node or remotely hosted on a version control system, for example, Git, which enables easy access and is shared between teammates.

Types of Ansible Inventory

1. Static Inventory

The common and static inventory contains groups and a confirmed list of hosts, which are defined inside a static file. The process of creating is very simple, and it is very efficient for small scale environments that have a small number of hosts.

But, in ever changing environments, the hosts change incessantly, so managing a static inventory is a very difficult task with a probability of mistakes.

2. Dynamic Inventory

The Dynamic inventory offers a fix for dynamic environments; some examples are cloud based infrastructures, where hosts are equipped and withdraw dynamically.

Instead of using a static file, dynamic inventory scripts create the inventory without interruption while running the program. It is done by querying outer sources like the configuration management database (CMDB) and cloud providers’ APIs.

3. Composite Inventory

A composite inventory joins both static and dynamic inventories; it allows users to gain the benefits of both dynamic and static inventories. This method is used, especially when partial hosts are relatively static and others are constantly changing.

It allows businesses to retain important parts, for example, network devices and physical servers, that are defined in a static inventory during the process of auto scaling, and cloud based instances are regulated dynamically.

How Ansible Inventory Works

At the time of performing Ansible reads the inventory file distinct with the usage of the -i or –inventory-file option. Ansible looks for a location that is defaulted to, for example, /etc/ansible/hosts, in the absence of an inventory file. After the loading of inventory, Ansible categorizes hosts into sets based on the distinct classes in the inventory file.

Groups are indicated by square brackets, and single hosts are enlisted below the group name to which they belong. Ansible aims single hosts or complete groups for operations, which makes it super easy to operate tasks on various systems at a time. Hosts relate to many groups, allowing logical classifications and the capability to do selected measures depending on these categories.

Furthermore, Ansible Inventory enables the usage of variables, enabling users to clarify distinct values and attributes for groups or hosts. These variables are used in playbooks to customize jobs depending on host characteristics or to offer configuration values.

Ansible Server Inventory: Manage Your Server Inventory with Ansible

In this section, we show you how to create a custom inventory file and execute commands using this inventory on a specific host and group.

Create a Custom Inventory File

By default, Ansible default inventory file is located at /etc/ansible/hosts. However, it is always a good idea to create a custom inventory file per-project. So you easily manage and execute your project using Ansible.

Let’s create a new project directory to hold your inventory file.

				
					mkdir project1
				
			

Next, create a custom inventory file under the project1 directory.

				
					nano project1/inventory
				
			

Add your all remote hosts IP addresses and hostname one per line as shown below:

				
					192.168.100.10
192.168.100.11
192.168.100.12
ubuntupc
centospc

				
			

Save and close your file when you are done. Then, run the ansible-inventory command to validate the inventory file.

				
					cd project1
ansible-inventory -i inventory --list
				
			

This shows you the information about your inventory file:

In the above inventory, you have not defined any group to identify remote hosts. So Ansible creates two distincts groups named all and ungrouped. The all is used to define all servers from your inventory file and ungrouped is used to define servers that aren’t specified within a group.

Now, let’s run Ansible commands using a custom inventory file.

				
					ansible all -i inventory -m ping
				
			

This command execute the ping command on all servers listed in your inventory file.

Grouping an Inventory File

For multi servers environment, you can organize your servers into different groups and subgroups to easily manage your servers. Grouping allows you to use group variables withing Ansible inventory file to manage multiple staging environments with Ansible.

Let’s create an inventory file and grouped them by different parameters.

				
					nano project1/inventory
				
			

Add the following configuration.

				
					[web-server]
192.168.100.10

[database-server]
192.168.100.11

[proxy-server]
192.168.100.12

				
			

Save and close the file then run the ansible-inventory command again.

				
					ansible-inventory -i inventory --list
				
			

You should see the information about all your servers by group name:

Ansible also allows you to define multiple groups as children under a “metagroup” group. Let’s create an inventory file to understand it better.

				
					nano inventory
				
			

Add the following configuration.

				
					[web1]
192.168.100.10
192.168.100.100

[web2]
192.168.100.11
192.168.100.111

[proxy1]
192.168.100.12
192.168.100.121

[proxy2]
192.168.100.13
192.168.100.121

[webservers:children]
web1
web2

[proxyservers:children]
proxy1
proxy2

				
			

Save the file then run the ansible-inventory command to see your inventory information.

				
					ansible-inventory -i inventory --list
				
			

You should see inventory information in the following screen.

Setting Up Ansible Host Aliases

Ansible also allows you to use aliases in inventory file to define server IP addresses and use this aliases during the playbook execution. Let’s create an inventory file and add a variable named ansible_host after the alias name. Each alias contains the corresponding IP address.

				
					nano inventory
				
			

Add the following lines:

				
					host1 ansible_host=192.168.100.10
host2 ansible_host=192.168.100.11
host3 ansible_host=192.168.100.12
				
			

Save and close the file then run the ansible-inventory command.

				
					ansible-inventory -i inventory --list
				
			

You will see the following screen.

How to Execute Commands Using a Custome Inventory File

In this section, we execute Ansible commands with patterns to target specific hosts, groups, or subgroups in your inventory file. Patterns allows you to execute commands only on the specified hosts or groups.

For example, to check all hosts in the proxy1 group, run the following command.

				
					ansible proxy1 --list-hosts -i inventory
				
			

You will see the following information.

				
					  hosts (2):
    192.168.100.12
    192.168.100.121
				
			

To see the hosts information about both proxy1 and proxy2 group, run the following command.

				
					ansible proxy1:proxy2 --list-hosts -i inventory
				
			

You see the following information.

				
					  hosts (3):
    192.168.100.12
    192.168.100.121
    192.168.100.13

				
			

If you want to see only servers that are both in proxy1 and proxy2, use the & pattern.

				
					ansible proxy1:\&proxy2 --list-hosts -i inventory
				
			

You see the common server in the following output.

				
					
  hosts (1):
    192.168.100.121

				
			

Execute the following command if you want to list servers in proxy1 except those also in proxy2

				
					ansible proxy1:\!proxy2 --list-hosts -i inventory
				
			

You see the following output.

				
					  hosts (1):
    192.168.100.12

				
			

To see the information about all web servers, run the Ansible command with child group.

				
					ansible webservers:children --list-hosts -i inventory
				
			

You will see the following output.

				
					  hosts (4):
    192.168.100.10
    192.168.100.100
    192.168.100.11
    192.168.100.111

				
			

Ansible Server Inventory: Manage Server Inventory with Ansible Conclusion

In this post, we created a custom inventory file and grouping them and use the petterns to execute Ansible command against specific hosts or groups. We can say that a valid Ansible inventory work as a base entity for a successful automation. With different types of Ansible, businesses customize their inventory operation to match their unique infrastructure requirements making sure efficient performance of Ansible tasks in both diverse and dynamic environments.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x