Telling Ansible To-Do things According To Operating System

Gupta Aditya
4 min readDec 22, 2020

--

So hey guys hope you all are doing today's article gonna be super useful and exciting as we all know in real-time we have to use different operating system due to some requirements but as we have seen in the previous article to download some package we have to give package name to ansible but just thin when you have two OS one RedHat and one ubuntu now in RedHat to install webserver we name is httpd but to install the same thing in ubuntu the name is apache2 but we want ansible to do things automatically and take decision that what is called automation right so this can be achieved easily and we have many methods to do this today we will discuss the manual method for this.

If you have not checked my previous article on ansible I recommend you to read that first in that we have talked about how to configure yum that is necessary for any practical and in that we have also covered how to automate docker using ansible.

If you have not checked my previous article on ansible I recommend you to read that first in that we have talked about how to configure yum that is necessary for any practical and in that we have also covered how to automate docker using ansible.

Once yum is setup we are ready but if you don’t want to configure yum you can use Cloud services like AWS, GCP, Azure etc. Today here I will be using Ec2 instance which service offered by Aws if you want to know how to use AWS instance from ansible then let me know gonna cover the whole article on it but if you don't use cloud services you can install it on your system and set inventory in the same way as we have discussed in previous article.

Let's start with this practice as below you can see the playbook lets understand what does that mean line by line:-

- hosts: diff
vars_files:
— “{{ansible_facts[‘distribution’]}}-{{ansible_facts[‘distribution_major_version’]}}.yml”
tasks:
— debug:
msg: “{{a}}”
— package:
name: “{{package}}”
state: present
— template:
dest: “{{dest}}”
src: a.html
— service:
name: “{{service}}”
state: started

First, we have to define the host group and then we have a variable file from which playbook take variable now you can see we have used {{ansible_facts[‘distribution’]}} -{{ansible_facts[‘distribution_major_version’]}} this mean whenever playbook run it go to target node and extract facts like os name its version and other information so we have to print it and tell playbook to use that file

for eg when I run my playbook it pings to IP which is of RedHat then under the variable file it gives Redhat-8.0 now playbook finds a file with this name and uses the variable specified in that file.

Files in Folder

As you can see in this image we have 3 files one playbook with the name i.yml other is os specific file that is used by Playbook after gathering facts.

All the codes with files will be in the GitHub link provided at the end of the Article.

As soon as you turn the playbook it automatically decides which package name to use according to the operating system that's what we need.😎

Server running on ubuntu os
Server running on amazon os

Guys, here we come to the end of this blog I hope you all like it and found it informative. If have any query feel free to reach me :)

Github link:-https://github.com/guptaadi123/Ansible.git

Guys follow me for such amazing blogs and if have any review then please let me know I will keep those points in my mind next time while writing blogs. If want to read more such blog to know more about me here is my website link https://sites.google.com/view/adityvgupta/home.Guys Please do not hesitate to keep 👏👏👏👏👏 for it (An Open Secret: You can clap up to 50 times for a post, and the best part is, it wouldn’t cost you anything), also feel free to share it across. This really means a lot to me.

--

--

No responses yet