Configuring platform based services using Ansible…

💬Use-case :

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

Lets lookup how this can be done:

Before discussing further , we need to know what are the 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

In our case , we will require to fetch OS distribution attribute to know which os we will be configuring ……..

#ansible all -m ansible.builtin.setup -a 'filter=ansible_distributions'    "ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.5.1804",
"ansible_dns": {
"nameservers": [


Now lets get started to write playbook:

#vim os_dependent.yml

In the above play :

➡we have included the var files named after OS distributions, thus dynamically ansible_facts assist us to get ansible_distribution .

➡Installing specified package in var file named as <os_name>.yml

#cat Ubuntu.yml

package : apache2
service : apache2
document_root : /var/www/html

#cat RedHat.yml

package: httpd
service : httpd
document_root : /var/www/html

🔴Be sure that , variable file name matches the ansible_distribution value….

After running the playbook,..

👉Ubuntu Instance launched on the top of AWS:

👉RedHat Instance launched on the top of AWS:

Now lets check servers are up or not …..

Thus ansible_facts plays vital role in gaining information about remote host which , helps to configure the remote system accordingly on the fly ………

Github repo for playbook :

Happy executing …………🖋🖋🤗



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Technological Enthusiast , Like to express what is need of time, Relates real world to philosophical insights