Dynamic Ansible Playbook for Configuring Web-Server in Any OS.

Pritee Dharme .
4 min readDec 23, 2020

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 ).

Hello…!!

I am back with another article in with we can see how ansible used to configure apache webserver in any type of OS . So let’s continue…

First let’s see little bit about the main part of our task that is ansible fact and variable.

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.

Ansible uses variables to manage differences between systems. With Ansible, you can execute tasks and playbooks on multiple different systems with a single command. To represent the variations among those different systems, you can create variables with standard YAML syntax, including lists and dictionaries.

Pre-requisite :

For doing this practical, you have to first install ansible and configure the control node. For that you can use my below article for reference..

Now, I am do this task on the top of AWS for that I am lunching 3 instance 1 for control node and 2 for target node.

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

Now after doing all this thing just for conformation ping to the target node using command..

“ ansible all -m ping ”

Here our target are connected with control node successfully.

After that we have to check your managed node’s facts using the below command. Also, I am attaching the screenshots of my both managed node’s facts which we will need here.

“ ansible -m setup (manage_node IP) ”

Redhat
Ubuntu

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.

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

Note : When you 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.

So let’s create the playbook for our two manage node that is for Redhat-8 and Ubuntu-20.

RedHat-8.yml
Ubuntu-20.yml

For implementing this task we are create the webpage and copy it to manage node. Here I create index.html.j2 webpage.

After creating these playbooks, you can create the main playbook which will be responsible to deploy the webpage dynamically.

main.yml

Now, we are creating the main playbook . So last part is run this playbook using command ..

“ ansible-playbook (playbook_name) ”

Here our both manage node are configured successfully we have to just open the webpage in browser for checking..

webpage for RedHat OS
webpage for Ubuntu OS

For any query in the playbook code then use my Github repo which is given below ..

I am doing this task successfully . And I hope with the help of this article you can also do your task without any error.

If you like this then clap and share it … :)

For knowing more about me connect with me in LinkedIn..

Thank you for visiting this article…!!

--

--