Introduction to Ansible

Danuka Praneeth
3 min readMay 19, 2018

--

Ansible is an automation technology that can be used for configuration management purposes. It is a simple and human readable language and can be used to manage infastructure across many platforms. Ansible is widely used to deploy complex and multitier applications and services accross cloud, virtual and physical environments.

Why Ansible?

1. Reduce repetition : Repetitive tasks can be automated with a simple ansile code, so that anyone can do the same task next time with a single ansible command.

2. Easy coding : Ansible coding is so simple that any beginner can understand ansible codes as they are written in a language that approach plain English

3. Tackle complex processes : Complex tasks can be easily handled with ansible as it supports scalability with consistency and reliability.

4. Continuous improvement : As we update the ansible scripts with deployment related modifications and improvements, we will be always having an up-to-date and accurate deployment script with time.

5. Minimum resource requirement : It does not require installation of a long list of softwares or repositories on the servers. It runs the code using SSH, with no agents required to be installed on remote systems.

6. One-to-many : Ansible will handle configuration management on many servers from a single machine. You can easily add machines to your infrastructure without too much trouble.

7.Save your time : If you have a good network connectivity, then ansible has a super fast execution capability for you.

Ansible architecture

Ansible deployment architecture

We need to have a server as the Ansible management node with Ansible installed to execute and Ansible script. You may use a destination node itself as the management node. You also need the remote server connectivity through SSH from the management node. Then you need to copy the Ansible scripts(roles) to the management node along with host inventory file and execution playbook. Then Ansible script can be executed with the following single Ansible command.

$ ansible-playbook playbookname.yml

Ansible playbook

Playbooks include the sets of commands (plays) to be executed and corresponding target groups (hosts). It is just similar to a list of instructions for the target nodes in the desired order. You must understand some key terms to run your Playbook successfully. I have explained those terms using the below sample playbook written to install ha-proxy on a remote server.

---
- name: Install haproxy
hosts: lb_server
become: true
tasks:
- name: install haproxy
yum: name=haproxy state=present
tags: haproxy
- name: start haproxy
service: name=haproxy state=running
tags: haproxy

name : This is a simple definition of the underline process execution and this will keep the Playbooks readable

hosts : Identifies the target host to run the Ansible scripts. You may use a specific IP of a target server or a server group name from the host file. Here I have used a server group name called ‘lb_server’ from the below host file. So this playbook will be executed on all the servers defined under the group lb_server in the host file.

become statements: ‘true’ statement is included here to ensure haproxy installed without a problem with super user privilege (it’s not always required)

tasks : Ansible commands are included in this section. It may be required to define the use of certain service module, conditions or argument values, certain variables that are being passed etc. You may follow Ansible command guidelines to understand and write new components to this section.

Host file (inventory file): This file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. Following is a sample host file which defines three server groups to execute the commands. You may use one or many of those server groups in your playbook against the ‘hosts’ parameter depending on your requirement.

[local]
localhost ansible_connection=local
[lb_server]
18.221.40.252
18.221.40.253
[back-end_server]
18.218.235.207
[all:vars]
ansible_connection=ssh
ansible_ssh_user=centos
ansible_ssh_pass=xxxxxxx
ansible_python_interpreter=/usr/bin/python2.7

I will explain Ansible roles and Ansible Galaxy through my next publication.

--

--

Danuka Praneeth
Danuka Praneeth

Written by Danuka Praneeth

Senior Software Engineer | BSc (Hons) Engineering | CIMA | Autodidact | Knowledge-Seeker

No responses yet