vineri, 24 iunie 2016

Small Ansible introduction

When you get into something new, the learning curve, depending on the propensity of the particular context of which one is in, might be more or less steeper.

If you are like me, depending of the subject matter at hand and the particular life situation, I end up wanting to dwelve into the core of the subject matter as fast as possible or just take my time to sip slowly through and through until I have a thorough understanding.

So to get to the point of our talk. What is Ansible all about ? Ansible is a configuration management tool and language which one can use to model and configure by automating the repetitive configuration patterns.

If you want to install and configure a particular software on 10 or 20 servers one can do this with Ansible in less time than it would take to do it by hand. Multiply this with the menial daily administrative duties and one can infer the amount of time saved by automating all these tasks.

Ansible is a tool that once installed and configured properly can connect remotely through ssh to all hosts and run the configuration tasks.

Learning Ansible begins with YAML, the language into which one has to model the configuration infrastructure. With YAML one can dwelve into writing roles which in themselves are the conceptual view of a particular infrastructure. Servers, configurations, tasks are all to be found into such a role.

Yaml is a descriptive language much like Python. Spaces matter because one diferentiates separate sections through indentation. One space less you get an error, one space more and the play runs just fine. A play is essentialy a sequence of steps and a sequence of steps is essentialy a list.

Creating a role from scratch is essentialy done by running: ansible-galaxy init name_of_our_role --force.

What you get by running the above command is a directory with the proper structure of an Ansible role:

defaults (variables), files (files that you want Ansible to upload remotely), handlers (triggers that you want to run, like starting, stopping, restarting services), meta ( useful information regarding the particular role, like the dependencies with other roles), tasks ( the list of steps that you want executed), templates (Jinja configuration files that will be filled in by Ansible) and vars (variables used across the Yaml files and the Jinja configuration files).

Yaml files start with --- and end up with ... and have an .yml extension. The ones from each of the role directories which Ansible knows how to read are called main.yml

YAML works with lists and dictionaries. To get up to speed one has to understand the syntax for them.

     - item1
     - item2

A dictionary entry is simply a pair of a name and a value. For example the name of a variable to be used inside a Jinja configuration file. Say you need to get a path to a certain location to be used inside the configuration file.

log_file_for_your_program: /var/log/your_program.log

To reference the said variable inside an YAML script or Jinja configuration file you just include it in double brackets:

{{ log_file_for_your_program }}

To get a list of variables that can be used inside YAML files and which Ansible knows how to interpret and use you can issue:

ansible -m setup ip_host -u remote_ssh_username

Once you get the role up and running you can run it on a group of hosts by defining a playbook.

The playbook is an YAML file with the following structure:

- hosts: name_of_hosts
       - name_of_our_role

Name of our hosts can be a list of hosts in a file called the inventory:


Once everything is in place you can just run the playbook with the following command:

ansible-playbook -vvv -u username -i inventory_file name_of_playbook

Niciun comentariu:

Trimiteți un comentariu