Configuring platform based services using Ansible…

os_dependent configuration

💬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:

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": [
"127.0.0.1"
]

},

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

Ubuntu Server
RedHat Server

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 …………🖋🖋🤗

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