Install and Configure Ansible on Ubuntu

Ansible is a configuration management tool that helps in controlling large number of servers from one location in an automated manner.

Ansible basically communicates over normal SSH in order to connect and control the remote machines / servers. Because of this feature of Ansible we only need to install it on the main controller machine and not on the client machines.

Therefore, any server that has an ssh port can be configured by an Ansible machine.

Steps for installing and configuring Ansible on Ubuntu are as follows:

  1. Make sure your Ubuntu machine is up to date with latest packages.
$ sudo apt-get update

2. Now we will install the Ansible PPA repository on the system using below command

$ sudo apt-add-repository ppa:ansible/ansible

3. Install ansible after successfully adding the ansible ppa repository

$ sudo apt-get install ansible

4. Check for ansible version after installation is done.

$ ansible --version

5. Generate ssh key in the ansible machine, which we have to copy to all the remote hosts for doing deployments or configurations on them.

$ ssh-keygen -t rsa -b 4096 -C “tushar@tushar-VirtualBox”

6. Copy the ssh key generated to the remote host using the below command

Note: Before copying the ssh key make sure that you are able to ssh the remote host where you want to copy the key

$ ssh-copy-id root@

***root@**** in my case is the remote host username & IP respectively.

7. Now we need to edit the “hosts” file of ansible by specifying the group of servers/remote hosts which we need to connect and perform operations on.

Open the hosts file for editing, I am using nano for editing.

$ sudo nano /etc/ansible/hosts

Add the servers as highlighted below

Note: [test-servers] is the group-name I have given, which will refer to all the servers listed under it (Here I have listed only one server i.e. under it)

8. Now as we have setup our “hosts” file and other configurations are done, let’s try a very simple ansible command, which will ping all the servers listed in the “hosts” file.

$ ansible all -m ping

“all” means all hosts

“-m” stands for module

“ping” is one of the module of ansible

Ooops!!!! an error came , we need to install “sshpass” in our Ubuntu.

Note: It’s not necessary that this error will appear for you.To resolve this issue just install sshpass using below command.

$ apt-get install sshpass

9. Again try running the ansible command for pinging all the servers.

$ ansible all -m ping

We are successfully able to ping the server using ansible.

Wola!!!!!!!!……You have successfully installed and configure Ansible on your Ubuntu machine.