15 Dec 2017

Install & Configure Ansible on Ubuntu

Ubuntu/linux 0 Comment

The best way to get Ansible for Ubuntu is to add the project’s PPA to your system.

We need to install the software-properties-common package, which will give us ability to work with PPAs easily.

$ sudo apt-get update

$ sudo apt-get install software-properties-common

Once the package is installed we can add the Ansible PPA by typing th e following command.

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

press enter to accept PPA addition.

We need to refresh our system’s package index so that it is aware of the packages available in the PPA then we can install the software.

$ sudo apt-get update 

$ sudo apt-get install ansible

Set Up SSH Keys

Ansible primarily communicates with client computers through SSH.

Create a New SSH Key Pair

If you do not have already an SSH key pair that you would like to use for Ansible administration.

We can create the RSA keys by typing below command

$ ssh-keygen -t rsa

You will be asked to specify the file location of the created key pair, a passphrase, and the passphrase confirmation. Press ENTER through all of these to accept the default values.

Your new keys are available in your user’s ~/.ssh directory. The public key is called id_rsa.pub. The private key is called id_rsa.

We can add the ssh keys into client machine by typing

$ ssh-copy-id username@IP_Address

Configuring Ansible Hosts

Ansible keeps track of all of the servers that it knows about through a “hosts” file. We need to set up this file first before we can begin to communicate with our other computers.

Open the file with root privileges like this:

$ sudo vi /etc/ansible/hosts

alias ansible_ssh_host=your_server_ip

host1 ansible_ssh_host=
host2 ansible_ssh_host=
host3 ansible_ssh_host=

We can put our configuration with YAML files start with “—“, so make sure you don’t forget that part.

Using Simple Ansible Commands

Now that we have our hosts set up and enough configuration details to allow us to successfully connect to our hosts, we can try out our very first command.

Ping all of the servers you configured by typing:

$ ansible -m ping all


host1 | success >> {
“changed”: false,
“ping”: “pong”

host3 | success >> {
“changed”: false,
“ping”: “pong”

host2 | success >> {
“changed”: false,
“ping”: “pong”