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.

AYUSH BAJPAI
4 min readJan 31, 2021

This is my 3rd task of ansible training given by respected vimal daga sir .

👍 Task description:

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.( Note: No need to use when keyword here. )

let’s see first understand some technical terms related to our task:

ANSIBLE FACTS AND VARIABLE

With Ansible you can retrieve or discover certain variables containing information about your remote systems or about Ansible itself. Variables related to remote systems are called facts. With facts, you can use the behavior or state of one system as configuration on other systems. For example, you can use the IP address of one system as a configuration value on another system.

Ansible facts are data related to your remote systems, including operating systems, IP addresses, attached filesystems, and more.

👉Prerequisite required :

  • for doing this task we have ansible installed on our system .
  • inventory should be configured.

we require three vm’s for this task: I have taken two Vm’s of redhat and one of ubuntu. our controller node VM is of redhat.

STEP 1):configure the ansible configuration file:

So write down the IP’s of your managed nodes in the inventory file as I wrote in key.txt in the above article. Here, I am using the RedHat-8 and Ubuntu-20 operating systems in the aws to perform this but you can perform on any type of operating system.

now we can see that our hosts are connected or not using following command

ansible all -m ping

as we are seeing it is ping pong so our hosts are connected successfully.

STEP 2) Retrieve the ansible facts

After that we have to retrieve our managed node’s facts using the below command. Also, I am attaching the screenshots of my both managed node’s facts which we will be needed here.

to retrieve facts use command

ansible -m setup ip address

So, "ansible_distribution" and "ansible_distribution_major_version" facts use here to know about OS name and its version so that we can create a dynamic playbook for deploying the webpage without using any condition.

after retrieving the facts we have to create a file according to os name and it’s version.

Now , we are create separate playbook for each manage node that we are using in main playbook.

Note : When we are creating a playbook that contains the information of your managed node, make sure your playbook name should be OS_name-OS_version.yml i.e RedHat-8.yml otherwise your main playbook doesn’t work.

😐 Step 3) writing playbook for our 2 managed node ,one for ubuntu and other for red hat.

For ubuntu:

content inside ubuntu-20.yml file

for redhat: content inside RedHat-8.yml file

for implementating this task I have created a file index.html in my controller node which I will use to copy to managed node for checking my web server as task3.html.

After creating these two playbooks I will create a playbook main.yml which is responsible for deploying my webserver according to os name.

now we have created the main playbook now it is the time to run my playbook and see the result

Before running the playbook my ubuntu:

apache 2 is not installed .

to run the playbook use command:

ansible-playbook main.yml

playbook has successfully run now check our managed node:

REDHAT-8:

file task3.html came to our managed node.

file accessed successfully

On our Ubuntu-20:

apache 2 installed.

file task3.html came .

file accessed from browser.

so task done successfully thank you.

hope you liked it.

--

--

AYUSH BAJPAI
0 Followers

ansible learner,coding enthusiast,learning new technology