Ansible Roles Best Practices

Ansible Roles Granularity

We recommend building Ansible roles that are highly reusable by configuration, but we also recommend pragmatism as our highest priority.

Roles for systems and applications designed to be standalone, or before the Cloud was around, are called "non-native cloud applications", often those are stateful applications that can not be clustered without sharing the underlying storage and in general are not designed to be recreated or distribute the load between servers.

Be pragmatic

For the "non-native cloud applications" apply pragmatism and don't make a big effort in creating reusable roles, instead, create a single role that makes all the necessary changes and configurations for the application even if it has actions available in other roles. It is better to have a working role than to spend a huge amount of time fixing complicated dependencies.

You can find an example of such a role in the installation of WordPress. That role configures Nginx, PHP, and WordPress and adds Let's Encrypt - Free SSL/TLS Certificates by requesting a certificate and automating the necessary renewal tasks.

Using a single role instead of three or four roles avoids the complexity that a single Nginx role will have, needing to support too many configurations to be reusable for WordPress and also allowing for a high degree of specialization.

Role dependency (meta folder)

An example of a role defined for re-usability is the add_package role1, it was designed to install default packages defined in all group_vars and it is also used as a dependency for the WordPress role used in the How to use Ansible and Terraform together tutorial.

By defining a dependency with parameters inside the meta folder, we add other roles that should be executed before.

The add_packages role is used at the begging of the playbook installing common software in all machines, and later on by using dependencies to add the specific packages needed for the role.

  - { role: add_packages,
    linux_add_packages_repositories: "{{ wordpress_add_packages_repositories }}",
    linux_add_packages_keys: "{{ wordpress_add_packages_keys }}",
    linux_add_packages_names: "{{wordpress_add_packages_names }}"
  1. Download the source code for the How to use Ansible and Terraform together tutorial. ↩︎

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Cloud Tutorials

Ansible Terraform AWS
This Ansible AWS tutorial shows how to use Ansible and its dynamic inventory AWS plugin to provision the software and configuration of infrastructure that has been created with Terraform.
Ansible logo
How To Install The Ansible Control Node in Ubuntu.
SSH, which stands for Secure Shell, is a cryptographic network protocol that allows for secure communication and data transfer between two computers over an insecure network. Usage: Key features of SSH: Applications of SSH: Next Steps:
Ansible logo
Handling multiple infrastructure environments with Ansible by targeting the environment tag that included in the mandatory AWS Tags
Ansible logo
Define and apply a company-wide consistent structure for all your Ansible Playbooks that allows for easy understanding and maximum reutilization
Javier Ruiz Cloud and SaaS Expert

Javier Ruiz

IT Wonder Lab tutorials are based on the diverse experience of Javier Ruiz, who founded and bootstrapped a SaaS company in the energy sector. His company, later acquired by a NASDAQ traded company, managed over €2 billion per year of electricity for prominent energy producers across Europe and America. Javier has over 25 years of experience in building and managing IT companies, developing cloud infrastructure, leading cross-functional teams, and transitioning his own company from on-premises, consulting, and custom software development to a successful SaaS model that scaled globally.

Are you looking for cloud automation best practices tailored to your company?

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram