Ansible: Configuring target node using variables based on OS family by dynamically loading variable files

Yashraj Oswal
5 min readJan 21, 2021

Introduction:

There are various linux distribution present, and every distribution have their own commands to install and setup packages. Ansible playbook has the capability to detect the OS family with the help of ansible facts, so we can use this capability to install packages in target nodes based on their Os family type. For this we need to create following files:

  1. Variable file named as os_name.yml (e.g redhat.yml)
  2. Main.yml file to call the variable file dynamically.

Let’s start with the setup..!

Following setup is been made in AWS instances.

Creation of Ec2 instances:

Now you need to keep in mind that all three instances must be created in same availability zone, and in security group you need to allow all traffic, or create your own custom rule.

Security rule as allow all traffic
Create three ec2-instances

Setting-up Ansible in controller node instance:

As ansible is coded in python language, we need to install the lastest version of python3. Command: sudo yum install python3

Next we need to install ansible using python pip3 as ansible is also an python library. Command: pip3 install ansible — user

Now that we have installed ansible successfully, let us create the inventory file and setup ansible.cfg file.

Creation of Inventory file:

Inventory file is the host IP database, which ansible refers to reach the target node.

In the host file, we need to give, destination Public Ip_addr, username, password as public key we created while creating ec2-instance and as our target node is Linux distribution we are using connection protocol as SSH.

So need to transfer the public key, you downloaded while creating instance. Make use of WinSCP application to transfer the key from your baseOS to Cloud instance.

Once transferred, we need to give to key read permissions, so that ansible can read the key.

Command: chmod 400 keyname.pem

Now the Inventory file is successfully setup..!

Creation of Ansible config file in Linux active Dir:

First you need to create a new directory in /etc.

Command: mkdir /etc/ansible

Now we need the create an ansible.cfg file inside ansible Dir.

In ansible.cfg file we need to give the path of Inventory file, path of the private key, and keep Host_key_checking as false because SELinux will create an issue while connection via ssh for the first time, so you need to keep it as False.

Next you need to create priviledge escalation, because Ansible uses privilege escalation systems to execute tasks with root privileges or with another user’s permissions. Because this feature allows you to ‘become’ another user, different from the user that logged into the machine (remote user), we call it become.

Now that we have done with configuration of ansible, now we can check the connectivity of controller node with target node.

Above adhoc command is used to check the connectivity, when we ping and get the reply as pong, we can confirm that connection has established successfully.

Let us now create the variable files and main ansible playbook..!

Creation of variable files and Main.yml ansible playbook:

1. Creation of RedHat.yml variable file:

Make sure you used the exact name of the file, I have used above, because ansible_facts when gathers the info abpve os_family it fetch the name as RedHat so we are using the same name convention.

2. Creation of Ubuntu.yml variable file:

Here also use the same guidelines used above…

3. Creation of index.html file:

We need to create an index.html file so that we can call the variable file name inside it, and will help us to rectify whether the packaged installed in the appropriate instance or not.

4. Creation of main.yml playbook:

**Imp: Create all these file in folder, so that ansible will find this file and can successfully execute the playbook.**

Now we are done with all the required set-up…!!!. Let’s run the ansible playbook.

Running Ansible playbook:

Command: ansible-playbook main.yml

Now that we have successfully executed the playbook with no errors, lets check target node whether set-up has installed or not.

Target node 1(RedHat Instance):

So httpd package has installed successfully, and services has also been started.

Target node 2(Ubuntu Instance)

Now that we have confirmed our target nodes are sucessfully configured with webserver, lets check index.html file.

Running index.html file on browser:

First we will check Redhat instance, we require public ip_addr of RedHat instance.

URL: http://3.235.166.219

As you can see, IP_addr of RedHat instance has sucessfully installed with httpd package and we can see the OS name on the webpage as well. This name it has fetch using “ansible_fact”.

Checking Ubuntu instance:

URL: http://3.239.221.145

As you can see, IP_addr of Ubuntu instance has sucessfully installed with apache2 package and we can see the OS name on the webpage as well. This name it has fetch using “ansible_fact”.

Thank-you…! Here we have successfully done with the setup.

Keep reading..! Keep Learning..! Keep sharing..!

--

--