Ansible Playbook Structure Best Practices

· Updated · 1 min read · Ansible 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 team has to decide for itself.

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

  • 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

Folder Description
group_vars/all Contains default values for variables that will be applied to all hosts, independently from its membership to other groups.
group_vars/tag_app_wp Contains values to variables from hosts in group tag_app_wp (AWS tag app=wp).
group_vars/tag_environment_pre Contains values to variables from hosts in group tag_environment_pre (AWS tag environment=pre).
group_vars/tag_environment_pro Contains values to variables from hosts in group tag_environment_pro (AWS tag environment=pro).
inventory Since we are using Ansible Dynamic Inventory, it has the inventory plugin configuration (aws_ec2.yml; older projects use the ec2.ini and ec2.py files).
roles/ Root roles directory
roles/linux/add_packages Role to install the default packages
roles/linux/host_name Role to set the hostname of the instance
roles/linux/hosts_file Role to modify local hosts file for resolver
roles/linux/pam_limits Role to set various pam limits for kernel configuration
roles/linux/wordpress Role to install WordPress
ansible.cfg Local ansible configuration
ditwl_pro.sh Sets environment variables for PRO environment
ditwl_pro.yml Playbook for PRO environment

#Ansible