Dynamically load the variable file to Ansible Playbook

Srasthy Chaudhary
3 min readSep 8, 2021

Let us create an Ansible Playbook which will dynamically load the variable file named the same as OS_name and just by using the variable names we can Configure our Target Node.

Launch the two instances on AWS :

Configure the host file:

Here we need to give the user name according to the distribution and also need the same aws key pair which have been used at the time of OS launching. Give your key name in .pem format.

Configuring ansible.cfg file :

Here we need to configure the inventory file to ssh the instances.

In a playbook, we may want to execute different tasks, or have different goals, depending on the value of a fact (data about the remote system), a variable, or the result of a previous task. We may want the value of some variables to depend on the value of other variables. Or we may want to create additional groups of hosts based on whether the hosts match other criteria. We can do all of these things with conditionals.

Ansible uses Jinja2 tests and filters in conditionals. Ansible supports all the standard tests and filters, and adds some unique ones as well.

Now we’ll be going to create a var file according to the OS name of the distribution.

  1. Create the variable file with the RedHat.yml name

Create another variable file with the Ubuntu.yml name

Now create the playbook to configure the webserver

After that, we have to write create the index.html file which we are going to copy to the root document.

Now let's check the list of the hosts

Here you can see lists of remote nodes.

Now we’ll go to ping all of them. To check the connectivity between the controller node and target node.

Here you can see we are successfully able to ping with all the target nodes.

Now run the playbook to configure the webserver.

Here we can see that playbook run successfully without any error.

Now go to the web browser and check whether the web pages are copied or not?

Here you can see the web pages are copied successfully.

Hope you liked and enjoy the reading.

--

--