How to Setup Ansible Inventory and How it Works (Tutorial). Well Ansible is a modern configuration management tool that is used for setting up and maintaining remote servers. Its minimalist design enables users to get up and run instantly. It also uses inventory files to keep track of the hosts that are part of your infrastructure and different ways that can be used to reach them for running commands and playbooks. This inventory can be set up in several contrasting ways based on the environment and project needs. In this guide we will introduce Ansible inventory with it’s main advantages then move onto inventory setup phase on Ubuntu 20.04
Let’s proceed with How to Setup Ansible Inventory and How it Works.
Firstly Ansible is an open source IT automation engine that mitigates hard work from the work life. It boosts scalability, consistency and reliability of your IT environment.
You can use this tool for automation in three different ways:
1. Provisioning – It helps you in setting up the servers required in the infrastructure.
2. Configuration Management
Changing the configuration of an application, device, or OS
Starts and stops services.
Install or update applications.
Implement a security policy.
Perform multiple configuration tasks.
3. Application Deployment – It makes DevOps effortless by automating the deployment of internally developed applications to your production systems.
Additionally Ansible is best known for automating IT environments hosted on traditional bare metal servers, virtualization platforms, or in the cloud. It can also be used for automating the configuration of a broader range of systems and devices, including databases, storage devices, networks, firewalls, and a lot more.
Moreover, the highly advantageous point about Ansible is that you can accomplish particular tasks even if you are not proficient in commands. All you need is to specify the state required for the system, and the rest will be taken care of by this tool.
Firstly Ansible tends to work against multiple systems in your infrastructure. This procedure is performed by making the selection from the portion of systems stored in the Ansible Inventory File saved automatically in a particular location. Then Ansible also enables you to specify a different inventory file using a particular option on the command line.
The inventory is not only configurable but also allows you to use several different inventory files at the same time.
In a nutshell, Ansible Inventory Files are the hosts and group of hosts upon which commands, modules, and tasks in a playbook tend to operate. Moreover, depending on your Ansible environments and plugins, the file is deployed in several different formats, including IMI and YAML.
Ansible Inventory Files categories
Inventory Files are divided into different parts that are as given below:
Hosts and Groups – Ansible Inventory includes hosts and group names that are used in classifying systems and deciding what systems are being controlled at a particular time and for a particular purpose. Here, you can put systems in more than one group. However, it is essential to remember that variables can come from all of the groups they are a member of.
Host Variables – As discussed above, you can effortlessly assign variables to hosts to later used them in playbooks.
Group Variables – You can also apply variables to the entire group at once.
Groups Of Groups and Groups Variables – Ansible Inventory allows you to make groups of groups and Group variables.
Default Groups – Ansible Inventory File consists of two default groups, namely all and ungrouped. While ‘all’ contains all the hosts, ‘ungrouped’ contains hosts that do not have another group apart from ‘all.’
The default path of the Ansible inventory is /etc/ansible/hosts. However, you can also create an inventory file at separate location. Let’s take a look at a sample inventory file. The inventory file is an INI like format its simply a number of servers listed one after the others.
You can enter the name of the group within a pair of square brackets and define the list of servers part of that group in the lines below. You can have multiple such groups defined in a single inventory file. This is so you can target a group of servers for any Ansible playbook to configure all of them at once. Finally, you can also have a group of groups. In order to define a group of groups follows the same pattern as group creation except in this case you must add a colon followed by the keyword children to the parent group’s name. In this case, I would like to create a group named all_servers that contains all servers in the group’s mail, db, and web. And so I defined the group name all_servers within the square brackets followed by a colon in the keyword children.
Let’s take a deeper look at an inventory file. For example, I have a list of servers named from 1 to 4 as shown in the following screen:
However, I would like to refer to these servers in Ansible using an alias such as web server, database server and mail server. I could deal this by adding an alias for each server at the beginning of the line and assigning the address of the server to ansible_host parameter.With ansible_host as an inventory parameter used to specify the FQDN or IP address of a server. There are other inventory parameters too. Some of the other key ansible parameters are ansible_connection, ansible_port, ansible_user, and ansible_ssh_pass.
Ansible Parameters
A brief explanation of each parameter is shown below:
ansible_connection defines how Ansible connects to the Target server. This is how we define a Target host we wish to connect to is a Linux or a Windows host. You could also set it to localhost to indicate that you would like to work with localhost and not connect to any remote host. If you don’t have multiple remote servers to play around with you can simply start with localhost into an inventory file.
ansible_port defines which port to connect to. By default, it is set to 22 for SSH. But if you need to change that for some reason you can set it differently using the ansible_port parameter.
ansible_user defines the user to make remote connections. If you need to change that define it as shown here.
ansible_ssh_pass defines the SSH password for Linux.
In this post, we will show you how to create Ansible Inventory with some examples.
One server running Ubuntu 20.04 for Ansible Control Node with a static IP address 192.168.0.99.
Two servers running Ubuntu 20.04 for Ansible Managed Nodes with static IP addresses 192.168.0.100 and 192.168.0.101.
A root user or a user with sudo privileges on each node.
Install Ansible on Control Node
By default, the latest version of Ansible is not included in the Ubuntu 20.04 default repository. So you will need to add the Ansible repository to APT.
Firstly install all required dependencies using the following command:
Following that you will need to setup an SSH password-less authentication between the Ansible control and managed node. On the Ansible control node machine, generate an SSH key with the following command:
ssh-keygen -t rsa
You should see the following output:
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory 'https://net.cloudinfrastructureservices.co.uk/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:pwV2R+of52iRcg9eiUepkhvVmwWmn4B2f5PZ3Rq9iDM root@linux
The key's randomart image is:
+---[RSA 3072]----+
| . o |
| + + o |
| o = * + .|
| . = = O B*|
| S O O %+*|
| + O.@.+o|
| . .E=.+. |
| .o |
| |
+----[SHA256]-----+
Now copy the generated SSH key to the Ansible managed nodes. Let’s copy an SSH public key to the first Ansible managed node:
ssh-copy-id root@192.168.0.100
You should see the following output:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "https://net.cloudinfrastructureservices.co.uk/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.0.100 (192.168.0.100)' can't be established.
ECDSA key fingerprint is SHA256:3LOmcVmMuWeHCcJ32xQI7ApmVGSUzktFH/XJRc/OHCs.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.100's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.0.100'"
and check to make sure that only the key(s) you wanted were added.
After that please copy an SSH key to the second Ansible managed node:
ssh-copy-id root@192.168.0.101
You should see the following output:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "https://net.cloudinfrastructureservices.co.uk/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.0.101 (192.168.0.101)' can't be established.
ECDSA key fingerprint is SHA256:3LOmcVmMuWeHCcJ32xQI7ApmVGSUzktFH/XJRc/OHCs.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.101's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.0.101'"
and check to make sure that only the key(s) you wanted were added.
Afterwards you will need to create an Ansible inventory file to define your managed nodes. First, create a directory to hold the Ansible inventory file:
mkdir project
In here create an inventory file with the following command:
cd project
nano inventory.txt
Add the following lines:
[webserver]
web ansible_host=192.168.0.100 ansible_user=root
[database]
db ansible_host=192.168.0.101 ansible_user=root
Save and close the file when you are finished.
A brief explanation of the Ansible inventory file is shown below:
web and db is the name of the Ansible managed node.
192.168.0.100 and 192.168.0.101 is the IP address of each node.
ansible_user is the user name of each node.
You can now list all your nodes added in the inventory file using the following command:
How to Setup Ansible Inventory and How it Works Conclusion
In this post, we explained how to setup an Ansible Inventory on Ubuntu 20.04. We also demonstrated some real life examples to manage and monitor remote nodes using Ansible Adhoc commands. The inventory is not only configurable but also allows you to use several different inventory files at the same time.
Please take a look at our Ansible content as well in here.
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.
00votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.