Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Repository Structure Tutorial

Introduction

When working with Ansible, it's important to maintain a well-organized repository structure. This ensures that your playbooks, roles, and other Ansible resources are easy to manage and understand. In this tutorial, we will go through a comprehensive guide on how to structure your Ansible repository effectively.

Basic Repository Structure

A basic Ansible repository should have a clear and consistent structure. Here is an example of a simple repository layout:

my-ansible-project/
├── inventories/
│   ├── production/
│   └── staging/
├── roles/
│   ├── common/
│   ├── webserver/
│   └── database/
├── playbooks/
│   ├── site.yml
│   ├── webserver.yml
│   └── database.yml
└── ansible.cfg
                

Each directory and file serves a specific purpose:

  • inventories/: Contains inventory files for different environments, such as production and staging.
  • roles/: Contains role directories, each corresponding to a specific role (e.g., common, webserver, database).
  • playbooks/: Contains playbook files that define the tasks to be executed.
  • ansible.cfg: An optional configuration file for Ansible settings.

Detailed Role Structure

Each role in the roles/ directory should follow a standard structure. Here is an example of the webserver role:

roles/
└── webserver/
    ├── tasks/
    │   └── main.yml
    ├── handlers/
    │   └── main.yml
    ├── templates/
    │   └── httpd.conf.j2
    ├── files/
    │   └── index.html
    ├── vars/
    │   └── main.yml
    ├── defaults/
    │   └── main.yml
    ├── meta/
    │   └── main.yml
    └── README.md
                

Each subdirectory within a role serves a specific purpose:

  • tasks/: Contains the main list of tasks to be executed by the role.
  • handlers/: Contains handlers, which are tasks triggered by other tasks.
  • templates/: Contains Jinja2 templates used by the role.
  • files/: Contains static files used by the role.
  • vars/: Contains variables specific to the role.
  • defaults/: Contains default variables for the role.
  • meta/: Contains metadata about the role, such as dependencies.
  • README.md: Provides documentation for the role.

Inventory Files

Inventory files define the hosts and groups of hosts on which Ansible will operate. Here is an example of an inventory file for the production environment:

[webservers]
web1.example.com
web2.example.com

[databases]
db1.example.com
                

Each section in the inventory file represents a group of hosts. You can also define variables for groups and hosts within the inventory file.

Playbooks

Playbooks are YAML files that define the tasks to be executed on the specified hosts. Here is an example of a simple playbook:

- name: Deploy webserver
  hosts: webservers
  roles:
    - webserver
                

This playbook deploys the webserver role on all hosts in the webservers group.

Ansible Configuration File

The ansible.cfg file is an optional configuration file that can be used to customize Ansible's behavior. Here is an example:

[defaults]
inventory = ./inventories/production
roles_path = ./roles
                

This configuration file sets the default inventory to the production inventory and specifies the path to the roles directory.

Best Practices

Here are some best practices for maintaining a well-organized Ansible repository:

  • Use a consistent naming convention for files and directories.
  • Organize roles and playbooks based on their functionality and purpose.
  • Keep your inventory files up to date and organized by environment.
  • Document your roles and playbooks to make them easier to understand and maintain.
  • Use version control (e.g., Git) to manage changes to your Ansible repository.