Ansible Playbook that will dynamically load the variable file named same as OS name and just by using the variable names it will configure target nodes.

In this article, we will create a dynamic Ansible playbook for deploying a webpage in any type of OS without using the “when” keyword.

The need for the above-mentioned playbook is that there are different name of the software and services in the different Operating Systems. So we cannot directly hardcode the name of software and services in the Playbook as it varies os to os.

For Example:

  • The name of Apache Web Server in Redhat 8 is httpd.
  • And the name of Apache Web Server in Ubuntu 20 is apache2.

So we will use concept of Ansible Facts and will get the name of the OS with version using facts.

Command to see facts is:

We can get the name of OS from Key “ansible_distribution” and the version of OS from Key “ansible_distribution_major_version” of facts.

Now we will create a vars file with os_name.yml and include it in our dynamic playbook.

Now we will dynamically include these var files in our playbook.

Run the Playbook.

It will configure the webserver in all nodes (despite different OS).

For Github Link Click Here.