Ansible is an open source configuration management, application deployment as well software provisioning tool that is used to deploy, configure & manage servers. Due to ease in using Ansible is one of the easiest & most popular automation tools. It uses YAML, which is easy to learn & does not require you to learn a complicated programming language like Ruby (used in puppet & chef).

Also, it does not require any special agent to be installed on client machines & only requires client machines to have python and ssh installed, both of these are usually available on systems.

Recommended Read: Ansible Tutorial: Introduction to simple Ansible commands

Also Read: Top 4 Automation & Configuration management tools

In this tutorial, we will learn to install Ansible on Ubuntu machines.

Pre-requisites

Before we move onto install Ansible on Ubuntu, let’s discuss the pre-requisites for Ansible

1- We need at least two machines, one will act as an Ansible server & other as a client. We can add as many as the client needed.

2- Password-less SSH should be enabled between the ansible server and client. The process to do so is as follows,

Firstly create an SSH keypair on Ansible server using the following command,

$ ssh-keygen -t rsa

Two files will be created with the above-mentioned command i.e. id_rsa & id_rsa.pub. Now we need to copy the public key i.e. id_rsa.pub to the client system. So there are two ways, you can either copy the file using SCP command or you can execute the following command from the server,

$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.1.100

where 10.10.1.100 is the IP address of your client machine. Similarly, copy the file to all the client machines. Now before we move on to install ansible on Ubuntu, make sure that we are able to ssh to the client without any password,

$ ssh root@10.10.1.100

Now let's move on to the installation part.


Install Ansible on Ubuntu

To get the latest ansible version, we need to enable the ansible repo first. Install the repo with the following command,

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

Once we have repository enabled, we can now install ansible server using apt-get,

$ sudo apt-get update -y && sudo apt-get install ansible -y


Configuring Ansible hosts

Next, we need to create an inventory file to add client servers. To do that we need to edit the file /etc/ansible/hosts & add the clients in the following syntax,

[group-name]

alias ansible_ssh_host=host_IP_address

where, an alias is a name given to hosts we adding & it can be anything, host_IP_address is where we enter the IP address for the hosts. For this tutorial, we are going to add 2 clients/hosts for ansible to manage, so let’s create an entry for these two hosts in the configuration file,

$ vi /etc/ansible/hosts

[test_clients]

client1 ansible_ssh_host=10.10.1.100

client2 ansible_ssh_host=10.10.1.200

Save file & exit it. We are using root to login to other servers but we can use other local users as well & we need to define it for Ansible whatever user we will be using. To do so, we will first create a folder named ‘group_vars’ in ‘/etc/ansible’

$ cd /etc/ansible

$ mkdir group_vars

Next, we will create a file named after the group we have created in ‘etc/ansible/hosts’ i.e. test_clients

$ vi test_clients

& add the following information about the user,

ansible_ssh_user: root

Note:- File will start with ‘—’ (minus/dash symbol), so keep a note of that.

If we want to use the same user for all the groups created, then we can create only a single file named ‘all’ to mention the user details for ssh login, instead of creating a file for every group.

$ vi /etc/ansible/group_vars/all

ansible_ssh_user: root

Similarly, we can set up files for individual hosts as well.

Now, the setup for the clients has been done. We will now push some simple commands to all the clients being managed by Ansible.


Testing hosts

To check the connectivity of all the hosts, we will issue a command,

$ ansible -m ping all

If all the hosts are properly connected, it should return the following output,

client1 | SUCCESS => {

“changed”: false,

“ping”: “pong”

}

client2 | SUCCESS => {

“changed”: false,

“ping”: “pong”

}

We can also issue the command to an individual host,

$ ansible -m ping client1

or to the multiple hosts,

$ ansible -m ping client1:client2

or even to a single group,

$ ansible -m ping test_client

This completes our tutorial on how to install Ansible on Ubuntu. If any having doubts or queries regarding this post, use the comment box below.

We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,

DigitalOcean - 100$ free credit & Linode - 100$ free credit

Check some Exclusive Deals, HERE.

Also, check out DevOps Book You should read section.