How to Install Ansible | Easy Guide

Hello, everyone In this Article we are going to learn how we can install ansible in Linux  in a very simple and easy step-by-step approach

Yellow Fishing Boat Blog Banner 46

I have created 3 machines as follow

1 For controller Machine and 2 For Worker Machine

  • My Controller-Machine IP is – 54.86.70.84
  • My Worker-1-Machine IP is – 52.201.192.62
  • My Worker-2-Machine IP is – 23.20.53.232

My First machine is the controller machine and my second and third machine is the Worker-Machine

We need to check whether we can able to ping Machine second & Machine third with the Machine one with the IP address and the Hostname methods

Checking with the IP based method to check the connection With the machine-1 and machine-2

For Machine -1

Screenshot 10 12

For Machine-2

Screenshot 11 13

As we can see that we can able to ping to both Machine-2 and Machine-3 With the IP from our controller machine.

Now go to the Controller Node machine and edit the /etc/hosts file and we need to add the entry of both the Worker-1-Machine And Worker-2-Machine,

We are doing this step because we need to set up the hostname based communication between the Controller Machine and the Node Machine

Go to the Controller machine and edit the /etc/hosts file and add the IP of the Machine-2 and Machine-3 with their hostname

Screenshot 12 13

Testing the hostname connection between the Controller and the node-1 machine

Screenshot 15 8

Testing the hostname connection between the Controller and the node-2 machine

Screenshot 16 7

Now we have tested that we are now able to ping the controller machine with the Worker-1-Machine and Worker-2-Machine now we are ready to start our ansible lab

We called ansible as agentless this means that we don’t need to install any external agent to work with the ansible. As ansible works on SSH and by default the SSH service is installed in every Linux kernel soo we call the ansible agentless. But the fact is that we still need SSH to make the connection soo ansible is not agentless according to us

How we can install Ansible in Controller-Node

First check on controller Machine weather we have SSH package here.

# rpm -qa | grep openssh*
Screenshot 17 8

Now check on Worker-1-Machine that we have SSH Package here

Screenshot 18 6

Now check on Worker-2-Machine that we have SSH Package here

Screenshot 19 6

As we can see that we have SSH Package on all of 3 machine

We need to set the root password on Worker-1-Machine

Screenshot 20 4

We need to set the root password on Worker-2-Machine

Screenshot 21 4

We are setting the root password on Worker-1-Machine and Worker-2-Machine because when the controller node will execute the tasks on Worker-1-Machine and Worker-2-Machine then they must know the passwords for the root of both the machine


How to Make Login password less

We need to Make the Login to the Worker-1-Machine & Worker-2-Machine password less for the Controller Machine because we don’t want to specify the password every time when we run the ansible commands.

How to Generate key in Controller Machine

With the help of this command, we can generate the key on the controller machine that key will go to Worker Machine in order to make connection pass wordless

# ssh-key-gen
Screenshot 1

Now go to .ssh directory using cd command

# cd .ssh/

Now we need to run following command in order to Install key to our Worker-1-Machine. This will prompt the password for the Worker-1-Machine

#  ssh-copy-id -i /root/.ssh/id_rsa.pub root@Worker-1-Machine
Screenshot 30 1 1024x218 3

Now we need to run the following command in order to Install the key to our Worker-2-Machine. This will prompt the password for the Worker-2-Machine.

# ssh-copy-id -i /root/.ssh/id_rsa.pub root@Worker-2-Machine
Screenshot 30 1 1024x218 1

Now we have make our Worker machine password less for controller machine

How to Install Ansible

First we need to Install EPEL script on our Controller machine this will give us extra packages

# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Screenshot 22 4

Sometimes we need to install python on the controller machine. With the help of # yum install python3 -y command we can install python in our Controller Machine. In my case, I don’t require to install python

Screenshot 25 3

Now execute the following command in order to install ansible in our controller Machine

# yum install ansible* -y

Just execute this command on the controller machine in order to install ansible on controller Mchine

Screenshot 23 2

To check weather ansible is installed or not execute # ansible —version command

Screenshot 24 3

Conclusion

If you want to learn Ansible in the most simple language then please click on this link

If you want to learn Linux in the most simple language then please click on this link.

This is the complete knowledge of how we can install ansible if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles

Leave a Comment