Learning Ansible

Is it worth it to spend six hours to reduce a 15-minute task to five seconds? That was my first question when I embarked on my Ansible automation rabbit hole. The short answer is that it depends on the situation. In most cases, I say yes. And that’s what got me down the path of learning and Ansible. I document my journey here for anyone else that is starting out from scratch trying to learn ansible.

I know it’s not front-end specific, but I figured it would highlight the process of diving into something new and trying to make sense of it in the early stages. Below, I’m going to describe my experience trying to learn enough Ansible as a complete beginner and touch on my process for learning anything new.

Step 1: Exposure

My process for learning new topics is usually the same. First is to get exposed to the top and concepts so that I can I can figure what prerequisites I’m going to need to look at.

In getting exposed, it’s all about getting familiar with tools, terms, and concepts so you can understand what you’re reading.

With Ansible, I dove into their intro docs and some beginner tutorials, and I smacked into a barrage of terms that I had to understand.

Some of these were stand alone like YAML and others were Ansible specific like playbooks. After writing down some of the things that I was trying to understand, I had a better sense of the lay of the land, and now knew what I was reading. And now it was time to kick the tires and figure out what questions learning Ansible would answer for me.

Step 2: Take it for a spin

Now that I was familiar with what I was reading and could make sense of the documentation and the blogs a bit more, I decided to take a stab at installing Ansible and creating my first playbook.

This step is really about knowing enough about it to set it up so that you can start trying and getting the feed back you need. For Ansible, it was installing ansible, setting up the initial configurations, and creating my first playbook.

    echo "My first playbook command"
    ansible-playbook scripts/remote-update.yml -e "branch=1.3"

To get going, I had to figure out three things.

1 - YAML: Ansible tasks and playbooks are made of YAML files with Jinja2 templates.

    # YAML is a superset of JSON (has a few more rules)
    # a comment here
    some_key: some_value
        of_some: nested
        key_value: pairs
        just_like: javascript_object
        - and_this
        - is_like
        - an_array
    and_it: uses_indentation
    instead_of: braces

2 - Jinja2: Most examples that you see will have a sprinkling of Jinja2 templating in there, and I had to figure it out so I could at least tell when I was dealing with YAML and when I was dealing with Jinja2

    some_variable_with_jinja2: \{\{ someVariableHere }}

3 - Initial Configuration: To do anything really with Ansible, you need and main config ansible.cfg file and a hosts file that has an inventory of the machines on which to run your plays.

    inventory = ./hosts
    host_key_checking = False

Above the host_key_checking = False just suppresses a warning that ssh throws when you’re connecting to manchine that you have not connected to before.

The main challenges I faced getting started

A lot of the introductory tutorials teach you how to write playbooks without going into the file organization structure covered in the official docs. And this approach gets you started quickly. But this is bad because most of the ansible examples that are put out by the ansible team are set up using the recommended folder structure which can make things appear magical.

Note the real power and magic is in the structure. I will talk about this in my next post.

Summing up what I learned this far

Ansible allows you to automate the configuration of your remote servers and the deployment of software to the those machines by creating playbooks, or instruction sets, filled with tasks, or specific instructions to make that happen.

These playbooks are written with YAML and Jinja2 templating language mostly, from what I’ve seen, and the tasks use modules to execute commands on your remote hosts over a secure shell connection.

More to come

In my next post, I will talk about the struggles that I had trying to use variables, with the single playbook approach that is covered by most tutorials. I will also include what I learned about Roles, variables, and the real power of an organized Ansible set up.

But for now, I hope this article gives you a good level exposure to what it’s like to get started with Ansible. These are some of the concepts and terms and tools that you will need to familiarize yourself with, and where the magic is and what you can look forward to once you graduate to doing Ansible in a more structured way.

Like this post?

Never fall behind again. Keep up with a weekly digest of the
best posts from here and around the web.