Ansible Tutorial

What is Ansible?

Ansible is an automation tool. It automates cloud provisioning, configuration management, application deployment, intra-service orchestration, rolling updates, and many other IT needs. It allows you to control different systems from one central location.

For instance, a system admin or release engineer has a lot of daily repetitive tasks like creating the VMs, configuring them, migration of code, and application deployment which is a little hectic task. So a smarter way to achieve this is by scripting it. This means the script will take care of the building of the entire infra. But it will need a calling script, a considerable amount of time to write the script, and also need maintenance. The script that was operating on Ubuntu 14.x is not suitable on Ubuntu 16.x, and vice versa for Ubuntu 18.x.

So this is where Ansible comes into the picture. This tool is simple, powerful, and most importantly agentless.

  • Simple because -- It offers simple architecture that anyone can understand
  • Powerful because - It can achieve the most complex infra needs
  • Agentless because - It does not require any installations on the client machine

Configuration Management

Configuration management is a systems engineering process for establishing and maintaining consistency of a product's performance, functional, and physical attributes with its requirements, design, and operational information throughout its life. It may include regular updates, patches, system upgrades, etc.

Host Files and Playbooks

Ansible manages the inventory in simple text files called Inventory file or Host file. It contains all the details about your remote machines which you want to manage with the Ansible controller. Ansible uses these hosts files to control the actions on a specific group of remote machines using playbooks.

Playbooks contain the steps which the user wants to execute on a particular machine. Playbooks are run sequentially and are written in YAML format. Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.

A sample inventory file might look like this:

[webservers]
www.example1.com
www.example2.com
[dbservers]
192.x.x.x
10.x.x.x

A sample play book may look like -

- name: update web servers
  hosts: webservers
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum:
    name: httpd
    state: latest

Let's get started with a simple lab setup

We will demonstrate using three Linux machines where one will be the Ansible controller machine, and the other two will be the client servers managed nodes.

ansible-introduction

Before we start please read these control node requirements and managed nodes requirements

Open Oracle VM virtual box manager and start three Ubuntu machines and ssh into all three machines.

Installing Ansible

You need to install the Ansible software on the machine that will serve as the Ansible control node. From your control node, run the following command

$ sudo apt-get update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt-get install ansible –y

Next, check the version of the installed software using command below

$ ansible --version

Here it should reflect the ansible version on your console.

reference: ansible documentation