Ansible — Getting Started
Originally posted on Medium
What is Ansible?
When I started my career as a DevOps, the approach to the Configuration Management scared me quite a lot. I was introduced to the magnificent and powerful Chef (and Ruby, of course), which gave me the hell of a headache. I remember that the first thing I noticed, was the complexity of Chef structure and, as far as it’s very intuitive, the concept of ‘cookbooks’ or ‘recipes’ or #whateveryoucallit made me waste a lot of time on reading tons of documentations.
Just for clarity, I don’t hate Chef: I like it and I like its ‘development’ oriented processes! There’s a nice community around it and plenty of professionals who constantly maintain it.
The question was though: “Is there a less complex and easier tool that can be used instead of Chef?” or “I don’t know anything about configuration management and automations. How can get started easily and quickly?” One answer: ANSIBLE.
Ansible is an awesome free tool for configuration management written in python and uses a very neat and clean structure.
Who worked with Chef knows that has to deal with stuff such as cookbooks, data bags, chef nodes, etc and it can all be managed with a tool called ‘knife’.
What makes Ansible awesome is the fact that it’s agentless and allows you to manage everything from your own laptop. Why is that? It’s as easy as 123: given a hosts file called ‘inventory’, Ansible creates a ssh connection between your laptop and the hosts listed within the inventory. Before talking further about the inventory file, let’s have an overview of what an Ansible structure looks like.
N.B. everyone has his own best practices so be aware that the following structure it’s the one that my team and I found the best.
- Roles — the cores of our configurations; either packages or configurations or templates it’s best if defined within your roles!
- Playbooks — these guys let you orchestrate your configurations running your Roles against the hosts listed in your inventory we talked about above!
That’s it! You may say “That’s it? Is that possible?”; yes, it is indeed and I’ll go through the following steps:
- Install ansible;
- Create your first role;
- Test it with test kitchen; and
- Create and run your playbook.
I usually work on Debian based systems so will always use apt-get to install new packages but, of course, what I do on Debian it’s absolutely reproducible on RedHat, CentOS or OSX. Ansible can be installed either by using the Linux’s repository or by using python pip:
$ sudo apt-get update $ sudo apt-get install ansible
$ sudo apt-get update $ sudo apt-get install python-pip $ sudo pip install ansible
You may see pip complaining about some missing libraries but it’s nothing to be worried about; you will find the missing library/libraries at the bottom of the installation log.
Ready to run your first Ansible command? Just run
ansible --version and you should be able to see something like this:
$ ansible --version ansible 1.9.4 configured module search path = None
Cool, isn’t it?
Ansible Role Structure
Before going further with our role, let’s take a look at the directory structure:
. |__ defaults | |__ main.yml | |__ meta | |__ main.yml | |__ tasks | |__ config.yml | |__ main.yml | |__ site.yml | |__ templates | |__ vhost.conf.j2 | |__ index.html.j2
I haven’t included the directories for our tests but we’ll get back to that very soon.
The defaults directory contains all the default variables used by the role and they are usually defined within a main.yml file. We’ll find out what a variable does and how we use it very soon.
Here’s where we’ll store our role dependencies and all the meta informations such as author, description, company, etc.
Tasks directory… I believe that the name is quite straightforward, isn’t it? Ansible will look for a main.yml file which will run your tasks one by one according to the order you chose for them.
The templates for your role, if any,come here. Ansible uses the Python Templating Engine: Jinja2.
There are many other feature that a role could use, such as the handlers, but I’ll talk about them with another post!
Next Article: Let’s build an Ansible Role