Ansible for Beginners: Introduction and Installation

 

Managing an infrastructure is a big challenge now a day. Most of the infrastructure is made of heterogeneous devices. To manage all these devices, you need special people and skill. To run all the device efficiently and to get the maximum benefits of those devices you have to build an automated process with the help of some automation tools. There are a number of tools available in the market among them my preferable tool is ansible. In this post I will explain what ansible is and how to install ansible on a Linux system. How you can run ad-hoc command, How to write playbook and lastly I will show you what is roles and how you can use roles in ansible. It will be a series of tutorial post which I’m going to publish time to time.

What is Ansible?

Ansible is an open source configuration management tool and is available in many flavors of Linux like Ubuntu, RedHat, CentOS, Debian, etc. Ansible is written in python and to run ansible it need python. It is also available for Mac.

Why Ansible?

The main advantage of ansible is its agent less features. You don’t need to install anything on the client/remote devices. If you have python installed in your devices and ssh is configured, then your ansible setup is ready to go.

Lab setup and Installation

This lab is based on three Linux servers, among them 2 have Ubuntu 18.04 LTS server and 1 have CentOS 7 installed. Ansible will be installed in one of my Ubuntu server.

node1         192.168.12.15 (where ansible will be installed)
ubuntu-server 192.168.12.11
centos-server 192.168.12.12

Perform the below steps to install ansible in Ubuntu 18.04 server.

sudo apt install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible

After installing you can check your ansible version by the following command.

ansible --version

ansible 2.6.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/labuser/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0]

It will give you the ansible version and the location of the default config file. Now our ansible server is ready, It’s time to configure ansible so that it can talk to my other servers. To establish a communication channel between your ansible server and the managing nodes you have to set up SSH and also exchange the ssh key between them. Python is another requirement for ansible so check python is installed in the client/remote server. if not then install the python package. SSH is already installed on those servers by default, you just need to create and exchange the key. On your ansible server  (that is node1 192.168.12.15) generate the key. (root is not mandatory you can use other user also. It’s better to put that user in sudo group)

ssh-keygen

This will generate the key in your ansible server. For simplicity, I remain the passphrase empty. Our key is ready. Now we will exchange that key to our other two servers.  Perform the following on node1 to exchange the key.

ssh-copy-id labuser@<YOUR_SERVER_IP>

After the password authentication the key will be exchanged. you will see a message Number of key(s) added: 1 after successfully exchanging the key. Exchange the key for your other server also.

The hosts file

Our SSH key is exchanged between the servers and the servers are ready to talk, but you need to tell ansible which servers or devices it should talk. Ansible has a file called hosts, which default location in a Linux system is /etc/ansible/hosts. In this file you can tell ansible that you should talk with these devices or in other hand, you can tell ansible that I want to manage these devices. There are many ways you can setup your hosts file. Open the file, you will see some example in that file. I will create one group in that file called nodes and put my other servers in that nodes group. Open the /etc/ansible/hosts file and paste the below lines at the top of the hosts file.

[nodes]
server1 ansible_host=192.168.12.11
server2 ansible_host=192.168.12.12

Save and exit from that file. Now my ansible is ready to talk to my other servers. Before running any ansible command Let’s review our steps.

  1. We install ansible on node1 (192.168.12.15) server
  2. From the node1 server, we generate the ssh key and exchange the key to our other servers (server1 & server2).
  3. Edit the /etc/ansible/hosts file on node1 server.

If you closely look at the activity then you will see that we work on only the node1 server, where we install ansible. Now everything is ready Lets run our first ansible command.

Ad-hoc command

open a command prompt and type the below command on node1 server.

ansible nodes -m ping

server2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
server1 | SUCCESS => {
"changed": false,
"ping": "pong"
}

The above command simply call an ansible module named ping. This is NOT ICMP ping, this is just a trivial test module that requires Python on the remote-node. If you get a pong back then your node1 server is talking to your other server through ansible. if not then go through the above process carefully. The structure of ansible ad-hoc command is like below

ansible <group/single node name> -m <module name>

ansible: Is the keyword

<group/single node>: This is the group name or the single node name or the remote device name where you want to execute command. Ansible will look for the name in the /etc/ansible/hosts file. I give my servers name as server1 and server2, you can give any name you like.

-m <module>:  Is the parameter for calling module followed by the module name. The main bread and butter of Ansible is its module.  Modules are small python code that executes on the client/remote devices. Whenever you install ansible over 450 module ships with your installation. If you are good in python, you can write your own module.

Lets try some other ad-hoc command like to check the memory information on the servers. Type the below command on node1 server.

ansible nodes -m shell -a "free -m"

server1 | SUCCESS | rc=0 >>
total used free shared buff/cache available
Mem: 985 126 211 1 647 689
Swap: 1970 0 1970

server2 | SUCCESS | rc=0 >>
total used free shared buff/cache available
Mem: 1838 384 1059 9 394 1260
Swap: 2063 0 2063

This time I’m calling the shell module and pass other arbitrary command as a module parameter. You can also check the disk space using the same command. Type the below command to check the disk space from node1 server. This time let’s use server1 instead of the nodes group.

ansible server1 -m shell -a "df -h"

server1 | SUCCESS | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
udev 464M 0 464M 0% /dev
tmpfs 99M 1.1M 98M 2% /run
/dev/sda2 98G 4.8G 89G 6% /
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/loop0 87M 87M 0 100% /snap/core/4486
/dev/loop1 87M 87M 0 100% /snap/core/5145
tmpfs 99M 0 99M 0% /run/user/1000

That’s a quick setup of ansible. Hope you enjoy the post. In my next post I will show you how to write playbook in ansible. Till then its goodbye.

About the Author: Imtiaz is working in a financial organization in Bangladesh and having experience in system, network and security administration. Feel free to contact with him on LinkedIn or Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *