How To Setup Ansible In Ubuntu 15.04

Introduction

Ansible is an automation tool like puppet or Chef tools. Ansible configuration consists of two parts: Controlling machine and the node. No agent is required to be installed on Node. Nodes are controlled with SSH and location of nodes is specified via inventory stored in ansible Controller.

In this tutorial, we will show you how to install and configure some of the basic commands of Ansible in Ubuntu 15.04.

Prerequisites

  • Tools: sshpass, Jinja2, PyYAML, parmiko, httplib2.
  • Controlling OS: Ubuntu 15.04
  • IP Address: 192.168.1.10/24
  • User: unixmen
  • Node OS hostname: cassandra
  • IP Address of Node: 192.168.1.11

Installation

Add ansible repo to your sever.

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

 um(003)

Update Server and install ansible:

sudo apt-get install software-properties-common
sudo apt-get update
sudo apt-get install ansible

um(004)

Ansible communicate with clients using ssh, so we have to create ssh key pair for communication with node. We will create ssh key and copy the key to the remote host. Username must be same for all of your node.

Create .ssh directory in your home directory.

mkdir /home/unixmen/.ssh

Generate rsa key for your ssh session, this key will be shared with node.

ssh-keygen -t rsa

um(014)

Simply press enter and leave all fields as default.

Now, Share this key with your node (i.e. 192.168.1.11)

ssh-copy-id -i ~/.ssh/id_rsa.pub unixmen@192.168.1.11

um(015)

Login to node and test your credentials, and you will notice that it will not ask for ssh password. :

ssh unixmen@192.168.1.11

um(012)

Now, we have to configure our inventory file for Ansible Controller.

edit /etc/ansible/hosts and put ip address of your hosts.

nano /etc/ansible/hosts

um(017)

[unixmen-nodes] Indicates the we have create to organize our Nodes. You can add multiple nodes as per your requirement.

Test your configuration with ping command.

ansible -m ping unixmen-nodes

Sample output:

192.168.1.11 | success >> {
 "changed": false, 
 "ping": "pong"
}

If more then 1 nodes are available, they all will get ping with this single command.

um(020)

To check Partition of both remote hosts:

ansible -m command -a "df -h" unixmen-nodes

um(021)

Let’s check uptime:

ansible -m command -a "uptime" unixmen-nodes

um(022)

Hostname of both nodes:

ansible -m command -a "hostname" unixmen-nodes

um(023)

Install ‘nginx’ on both nodes.

 ansible -m command -a "sudo apt-get install nginx" unixmen-nodes

That’s all for now. Hope this tutorial will help you.

Cheers!