How to install and configure Ansible on ubuntu 20.04

In this article we will learn how to install and configure Ansible master and slave on Ubuntu 20.04 with easy and understandable steps.

So, let’s start it

What is Ansible

  • Ansible is open-source configuration management tool
  • Ansible used to do configurations on remote machines, such softwares installation.
  • Ansible can do wide range of automations on many remote machines at the same time
  • Ansible invented by Michael DeHaan and in 2015 Red Hat acquired Ansible

Why Ansible

  • Ansible is free and open-source tool.
  • Ansible helps to automate repetitive task with very easy way.
  • Ansible is client less- we do not required to install any client on remote machines
  • Ansible save our huge amount of time by doing automation.

Prerequisites

  • Install Python

Please ensure that python installed on the both the machines i.e Master and Slave, by default AWS Ubuntu images have python installed. You can skip this step if you are setting up ansible on AWS, If you have machines on other platform then install python using the following command.

sudo apt-get install python3
  • Enable keyless SSH access in between Ansible Master and Slave machines

You can choose any machine as a master and generate ssh-key. Use below command

ssh-keygen 

Once you use above command, keep the default values and keep on pressing enter, till you reach the following screen.

  • Copy generated public key. use below command for that.
sudo cat ~/.ssh/id_rsa.pub

  • Once you get public key, copy that and paste it in the slave machine’s “authorized_keys” file

You can do this using below command, it will open authorized_keys file and you have paste that key in it.

nano ~/.ssh/authorized_keys
OR
vi ~/.ssh/authorized_keys

Now, insert or paste copied public key in the new line. make sure don’t delete any existing keys from authorized_keys file then save and exit.

By adding public key from master to slave machine we have now configured key less access. To verify you can try to access slave machine and use command as mentioned in below format.

sudo ssh username@IP-address-of-salve machine

Verifying above ssh connection from master to slave we have configured our pre requisites.

Installation of Ansible on Master Machine

  • Use below commands to install Ansible on the master machine.
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
  • Configure slave, for that create host file on master machine. Use below command.
sudo nano /etc/ansible/hosts
OR
sudo vi /etc/ansible/hosts

Once you are inside the hosts file use below syntax to add a slave machine at the end of file. In this syntax, servers is the group and server1 is the slave machine’s name, you can give any name of your choice.

[servers] 
server1 ansible_host=slave-ip-address

Test Ansible Master Slave Connection

Use below command to check Ansible master slave connections.

ansible -m ping all

If you all configuration is correct then you would get below output.

Conclusion: So, this way we have covered, Ansible installation on Ubuntu machine and tested Ansible master slave connectivity.

Reference:

Ansible official documentation

Ansible official website

Related Article:

Don’t know what is docker? check here with easy steps

Don’t know how to check Linux machine health? click here.

DevOps Article

DevOps Article. Your Learnings Hub.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.