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.

Tamim Dalwai
3 min readMar 30, 2021

For doing this practical we require three operating systems, RHEL, Ubuntu and one for Controller Node. We need to have ansible installed and configured in our controller node.

Here, I am using the RedHat-8 OS, For doing this practical we need to know about ansible-facts.

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.

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.

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

We are created 3 YAML files such as main.yml (the main code of configuration), RedHat-8.yml (variables for rhel-8) and the Ubuntu-20.yml (variables for ubuntu) and one index.html.j2 file.

RedHat-8.yml:

Ubuntu-20.yml:

index.html.j2:

Playbook — main.yml

Inventory file for the ansible

ansible.cfg file to configure the inventory path

ansible-playbook main.yml

is a command used to run the main.yml playbook

as you can see playbook run successfully

Final Output

httpd service is started in the RHEL-8

index.html.j2 File is uploaded in the Document root of apache-webserver

It also has connection and you can see the OS name and Package Name

Thank — You for Reading…

--

--