How to Structure Your Ansible Playbook? — the Minimalistic Guide

Amroj Sandhu
thecloudbee
Published in
3 min readJan 26, 2021

Ansible helps us to setup or configure 100s of machines in an agent-less manner. This guide will help you structure the Ansible Playbook such that it is more intuitive — the team will love to collaborate.

By Hal Gatewood from unsplash

Why a structure is important in Ansible?

Ansible is the one destination for all IT automation. With time, the complexity of a system increases and so does the number of components involved. It becomes important to isolate the components in your automation scripts. These scripts work independently of each other — called roles in Ansible.

The Single Responsibility principle when applied to an Ansible folder structure makes a lot of sense. There are different ways to separate your components. Below is the one that always works for me.

Ansible Roles to define the limits

Roles help you define a directory structure such that — everything under a single role can share variables and tasks. Once you group the content into roles, it can be reused.

Let’s dive into the Ansible structure. Consider a typical scenario where we are creating a Kafka cluster with several Kafka and Zookeeper nodes.

Deployment Plan

Keeping in mind the above deployment plan, we can point out that Java must be a different role altogether. The Java role will be included both in Zookeeper and Kafka playbooks.

The minimal folder structure to contain the above requirements.

We have three different roles Java, Kafka and Zookeeper. Each of the roles acts as an independent entity and can run in isolation.

Ansible Host Inventory

Ansible works with a spectrum of systems in infrastructure at the same time. Consider the below two use cases.

  1. A change to be applied just to the Zookeeper nodes in production.
  2. A change to be applied to all the staging nodes.

Ansible hosts inventory enables you to group your resources. Further the :children postfix in the hosts file lets you have groups of groups.

We have grouped Kafka and Zookeeper hosts based on the services we want to install and the flavors.

Ansible Playbook to put everything together

Playbook helps you put together the roles and hosts.

The below command gives us a clearer picture of how everything can be put together. We can use the --limit argument to limit the groups to run the Ansible.

Or we can say --limit 'production' to deploy the cluster on the production environment.

Conclusion

A complex infra can lead to complicating our Ansible scripts. It is trivial to define a directory structure that is scalable. By having a folder structure and a host inventory we can make the best out of our Ansible automation.

Originally published at https://www.thecloudbee.blog on January 26, 2021.

--

--