Configuring Ansible Inventory
Manuj Aggarwal
Top Voice in AI | Helping SMBs Scale with AI & Automation | CIO at TetraNoodle | AI Speaker & Author | 4x AI Patents | Travel Lover??
What is an Ansible Inventory?
The Ansible Inventory is a highly customizable component of Ansible. It is a configured location where you can place credentials of the remote hosts, such as IP Address and Username. With Ansible, you can effortlessly classify or group nodes depending on the type of application being used. Ansible Inventory is highly customizable, and can efficiently work with dynamic sources such as the cloud.
Ansible allows you to work with multiple inventory files at the same time. This way, you can accomplish a number of tasks in a limited project timeline.
How to Create an Ansible Inventory?
Creating an Ansible inventory is easy. It is just a plain text file written in YAML format. Optionally, you can also write them in JSON format. Any text editor or IDE can be used to write these files. Here we are using Microsoft Visual Studio Code.
You can execute node classification based on Operating System type, to find out the operating systems of the two remote hosts.
Let’s create a new inventory file here. On the left-hand side panel, at the top, click the New File icon next to WORKSPACE. Let’s name this file as inventory. The moment you start typing on this page, it shows inventory as an open editor on the left-hand pane. This makes it easy to keep track of the editors that are in progress.
In the inventory file, input the IP addresses and usernames for the target remote servers you intend to configure using Ansible. This way, it informs Ansible that these are the servers to be managed.
Let’s say you have two servers to manage – one is an Ubuntu machine, and another one is a Fedora machine. On the first line, you can enter the IP address of the Ubuntu VM, followed by the variable for the username along with the input value: 192.168.180.131ansible_ssh_user=tetra-noodle. On the second line, let’s enter the same variable for the Fedora VM, that is IP address, followed by variable for username and then the input value: 192.168.180.136 ansible_ssh_user=tetra-noodle. You can easily check for reachability of the remote servers through the inventory file, by using the Ping Test.
Executing this test is as simple as running the command:
ansible all -m ping -i inventory
Here ansible is the main command. “all” stands for all target hosts.
Hyphen M stands for the name of the Ansible module being used – which in this case is ping.
Hyphen I stands for the inventory file.
This screenshot shows a successful ping test conducted via Ansible.
You can also classify the target nodes into groups of servers. To understand the concept of node classification better, you can use a rule to classify the entries, say, using server type. For instance, let’s say that you want to host a web server on the Ubuntu VM, and a MySQL database on the Fedora VM.
To start with, you need to configure the web server on the Ubuntu VM, replace ubuntuwith a web server in the square brackets. Let’s configure the database server on the Fedora VM by replacing fedora with dbserver.
Now, you can run the ping test for the web server and database server. You can use the integrated terminal in Visual Studio Code to run these commands.
The ping test commands you can use for the two servers are:
For the web server
ansible webserver –m –i inventory
For the database server
ansible dbserver –m –i inventory
Using this feature of Ansible, you have the flexibility in specifying the nodes on which you want to run the Ansible Playbooks.
You can efficiently run your IT configurations using the inventory file. You can use Ansible to run multiple environments seamlessly. Ansible works smoothly with multiple cloud platforms and data center infrastructures. This helps you achieve your business goals for IT innovation and progress. Ansible’s deployment solutions are exactly what your IT automation processes need!
If you want to learn ansible from scratch then our training program “DevOps: Hands-On Guide To Automation With Ansible“ is for you.
These DevOps technical skills are in high demand, but there’s no easy way to acquire this knowledge. Rather than rely on hit and trial method, this course will provide you with all the information you need to get started with your Ansible and DevOps projects.
What are you waiting for? Just click on the “Add to cart“ button. It’s time to take action. Become an expert DevOps engineer and generate a more significant income for you and your family.
Still not convinced? Take a look at our “Beginners’ Guide To Automation With Ansible”It is absolutely free of cost, and you will get a gist of what you will be gaining from the main course.
Let’s do this together!