Ansible Playbook that will dynamically load the variable file

Ananya Jain
3 min readMay 2, 2021

🔅 Objective : Create an Ansible Playbook which will dynamically load the variable file named same as OS_name and just by using the variable names we can Configure our target node.

First we have to install ansible in our controller node through pip3 as is a python module and written in python language.

pip3 install ansible

Then we have to create our inventory file which contains information of all our managed node.

After that we have to configure ansible.cfg file. This file contains the location of our inventory file ip.txt .

To check the list of hosts :

ansible all --list-host

We have controller node with IP Address 192.168.43.67 and managed node with IP Address 192.168.43.137. Let’s check the connectivity between two:

ansible all -m ping

There are two pre- required yml files before running the main playbook:-

  • RedHat.yml
  • Ubuntu.yml

These are the variable files for main playbook.

RedHat.yml

Ubuntu.yml

For creating a playbook first we have to know the steps so that we can easily convert those steps into YAML codes in playbook using ansible document.

Now let’s create ansible playbook using YAML format:

Note : While creating playbook make sure you have proper indentation !!

For more detailed information we can increase the verbosity by adding -v .

Now let’s run the playbook :

ansible-playbook -vv task3.yml

Playbook runs successfully !! Now let’s check whether httpd container is running on the top of our managed node.

Now let’s check our webpage by entering the IP Address with the file name on google :

192.168.43.137/index.html

This is our webpage !!

Thank You!!

--

--

Ananya Jain

Google Cloud ACE Certified | Google Cloud Student Mentor | Operation Team @Amazon Alexa Community | Aviatrix Certified Engineer | Machine Learning enthusiast