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
Update Server and install ansible:
sudo apt-get install software-properties-common
sudo apt-get update
sudo apt-get install ansible
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
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
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
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
[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.
To check Partition of both remote hosts:
ansible -m command -a "df -h" unixmen-nodes
Let’s check uptime:
ansible -m command -a "uptime" unixmen-nodes
Hostname of both nodes:
ansible -m command -a "hostname" unixmen-nodes
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!