Create a dynamic Ansible playbook for deploying a webpage in any type of OS
By: Sachin Kumar
Tags: Ansible Playbook, Redhat Linux, Ansible, Ubuntu, Httpd

Create a dynamic Ansible playbook for deploying a webpage in any type of OS

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

Welcome back to another article, here you will see the usage of Ansible facts and creation of a dynamic Ansible playbook for deploying a webpage on any type of operating system. So let get started.

Ansible Facts

Ansible facts are data related to your remote systems, including operating systems, IP addresses, attached filesystems, and more. You can access this data in the ansible_facts variable. By default, you can also access some Ansible facts as top-level variables with the ansible_ prefix. You can disable this behavior using the INJECT_FACTS_AS_VARS setting. To see all available facts, add this task to a play.

Pre-requisite

To perform this practical, your Ansible controller node should be configured. So if you aren’t comfortable with this part, then you can visit the below-mentioned article.

So write down the IPs 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.

After creating the inventory file, 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 managednode_ip

Facts of Ubuntu Operating system

Facts of RedHat Operating System

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 you can create a separate-separate playbook for your managed nodes that you are using.

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, create a separate playbook for all managed nodes like this.

Playbook for RedHat-8

Playbook for Ubuntu-20

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

So after creating the main playbook, run it using the below command.

ansible-playbook main.yml

Now you can see both managed nodes (RedHat and Ubuntu) have configured and the webpage has deployed as the below clip is showing.

If you have any problem with the playbook code then you can visit my Github repo which is mentioned below.

I tried to explain as much as possible. Hope You learned Something from here. Feel free to check out my LinkedIn profile mentioned below and obviously feel free to comment. I write Cloud Computing, Machine Learning, Bigdata, DevOps and Web, etc. blogs so feel free to follow me on Medium.

Thanks, Everyone for reading. That’s all… Signing Off… 😊