Ansible Playbook Structure Best Practices

Ansible Playbook Structure

Ansible Playbook structure is defined in official documentation but the recommended way to group hosts and apply roles is something that each user would have to decide for itself.

Define and apply a company-wide consistent structure for all your Ansible Playbooks that allows for easy understanding and maximum reutilization.

  • Avoid using individual hostnames to select hosts. In the cloud, all hosts should be treated as cattle, not as pets.
  • Create groups of hosts by:
    • Operating System and major release
    • Application and application release
    • Environment
  • Give safe defaults to all roles and use the group_vars to redefine values
  • Use a single playbook definition file for each environment
FolderDescription
group_vars\allContains default values for variables that will be applied to all hosts, independently from its membership to other groups.
group_vars\tag_app_wpContains values to variables from hosts in group tag_app_wp (AWS tag app=wp).
group_vars\tag_environment_preContains values to variables from hosts in group tag_environment_pre (AWS tag environment=pre).
group_vars\tag_environment_proContains values to variables from hosts in group tag_environment_pre (AWS tag environment=pro).
inventorySince we are using Ansible Dynamic Inventory, it has the ec2.ini and ec2.py files.
roles\Root roles directory
roles\linux\add_packagesRole to install WordPress
roles\linux\host_nameRole to set the hostname of the instance
roles\linux\hosts_fileRole to modify local hosts file for resolver
roles\linux\pam_limitsRole to set various pam limits for kernel configuration
roles\linux\wordpressRole to install WordPress
ansible.cfgLocal ansible configuration
ditwl_pro.shSets environment variables for PRO environment
ditwl_pro.ymlPlaybook for PRO environment

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
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 […]
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