Telling Ansible To-Do things According To Operating System

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.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Getting Started with the Polkadot API

Heroku — Spring Boot App

My journey to being AWS Solution Architect Associate Certified

Using Katalon Studio to handle multiple browser testing

Why I love testing (and why you should, too)

IBM DevOps and Software Engineering Professional Certificate(Offered by IBM)

String Representations in Python — Understand repr() vs. str()

Package Principles

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
Gupta Aditya

Gupta Aditya

More from Medium

Ansible Tutorial for Beginners: Playbook & Examples

Install Zoom flatpak in Debian-like systems — Ansible module flatpak

Automating A Virtual Fabric with Ansible, Terraform, GNS3 and FRR

Useful Kubernetes Tools