# Ansible Playbook Structure Best Practices

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

- Source: https://www.itwonderlab.com/ansible-playbook-structure-best-practices/
- Published: 2023-09-07
- Updated: 2026-10-05
- Author: Javier Ruiz Jiménez (https://www.javierruizjimenez.com/)
- Site: IT Wonder Lab (https://www.itwonderlab.com/)

---

## 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](https://www.itwonderlab.com/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 |
