Ansible Inventory File At A Glance
Ibraheem Ejibode
DevOps & Cloud Engineer | AWS, Terraform, Kubernetes | Pharmacist with a Passion for Technology and Healthcare Solutions
HOW TO WRITE YOUR ANSIBLE INVENTORY FILE
Ansible is a configuration management tool, that provides open-source automation that reduces complexity and runs everywhere. Using Ansible lets you automate virtually any task. Here are some common use cases for Ansible.
Ansible uses simple, human-readable scripts called playbooks to automate your tasks. You declare the desired state of a local or remote system in your playbook. Ansible ensures that the system remains in that state.
For further information on Ansible click on this link ansible documentation.
For this article, I will be using Vagrant, creating four VMs on my virtual box, two Ubuntu and two CentOS. Your vagrant file should be like this.
The next step is to run the command below, this command creates and configures guest machines according to your Vagrantfile.
vagrant up
The output should look like this:
SSH into the control machine where Ansible will be installed by running this command
vagrant ssh ansible
To install Ansible on this machine, run the following commands
sudo apt update
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
The output should look like this
It is important to know the version of ubuntu you are running, to check, run this command
lsb_release -a
output should look like this:
Now, vagrant ssh into the three other servers, which I named srv01, srv02, and srv03 from my vagrantfile , create a user in each of them(I named my user devops, but you can actually give any name), and give the user a password(you can give the same password). Run the following commands to execute the steps above. I will use srv02 server as an example, but the process is the same for srv01 and srv03
vagrant ssh srv02
sudo -i
useradd devops
passwd devops
We must give the new user(devops) sudoer privileges, run this commands:
visudo
Then, edit the file as shown below
Furthermore, we need to authenticate the password ins sshd_config file, the password authentication should be yes.
vi /etc/ssh/sshd_config
After this change, we need to restart the service by running this command:
service sshd restart
Output should look like this:
领英推荐
Note that to restart service in centO, the command is;
service ssh restart
Before we write our Ansible inventory, let us test the connectivity between the Ansible machine and the other three servers (srv01, srv02, srv03).
cd into the Ansible machine and run these commands:
Now that we have been able to ascertain the connection between the Ansible and the servers, we can create our Ansible inventory.
Ansible automates tasks on managed nodes or “hosts” in your infrastructure, using a list or group of lists known as inventory. You can pass host names at the command line, but most Ansible users create inventory files. Your inventory defines the managed nodes you automate, with groups so you can run automation tasks on multiple hosts at the same time. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against.
Create your project directory (named ansible_repo) on Ansible machine, in the home directory, and cd into it.
mkdir ansible_repo
cd ansible_repo
In this directory, we keep our ansible data, create a new directory here, and name it exercise1
mkdir exercise1
cd exercise1
cd into exercise1 and create an inventory file there using vim or any editor of your choice. Put the hostname, host IP address, and password in the inventory file as shown below
vim inventory-qa
Then test the connectivity using this command:
ansible -i inventory-qa -m ping web01
Output:
Let us test for web02 as well:
ansible -i inventory-qa -m ping web02
Output
Now, let us try to restructure our inventory file by creating group of groups and by inserting our hosts IP addresses in /etc/hosts
sudo vim /etc/hosts
Output:
Our inventory file should look like this now
Let us try to connect again by running this commands:
ansible -i inventory-qa -m ping websrvgrp
ansible -i inventory-qa -m ping dbsrvgrp
Congratulations! it is as simple as that. I would want to stress that having your password in the inventory file is not a best practice and it is very dangerous. In my next article, I will show how we can connect using ssh before we start writing our Ansible playbooks.
Thank you for reading!
Experienced DevOps & Infrastructure Engineer | Software Engineer | Expertise in GCP, AWS & IT Support | Driving Cloud Solutions & Automation
9 个月Well done
Jesus Is Lord ? Senior DevOps Engineer ? AI Engineer ? Technical Writer ? Resume Writer ? SDG 4 Advocate ??
9 个月Amazing one Ibraheem Ejibode
DevOps Engineer Driving Innovation in CI/CD Pipelines & Cloud Environments | Linux | Docker | Jenkins
9 个月This is nice.. Well done
On a mission to help 2000 Coaches and Professionals bring their visions to life; I help them save time to make more $$ through Personal Branding| LinkedIn Ghostwriter|Copywriter|| I inject your VISIBILITY with STEROIDS.
1 年Detailed, spot on article here Barakallahu feeh Ibraheem Ejibode
Building Bvida
1 年My Chief! Very well done, Sir. A very interesting piece indeed.