Ansible directory structure for efficient work

There is a lot of ways you can structure your playbooks, inventory, roles, and collections in Ansible, but it’s beneficial to find a structure that is suitable for both development and AWX/AAP. This is what my layout looks like:

$ tree -a playbooks/
playbooks/
├── .gitignore
├── ansible.cfg
├── collections -> /home/jiholland/ansible-collections
├── inventory -> /home/jiholland/ansible-inventory
├── example_1_playbook.yml
├── example_2_playbook.yml
├── example_3_playbook.yml
└── roles -> /home/jiholland/ansible-roles

I have a Git repository called playbooks that contains all my playbooks. Symbolic links point to my roles, collections, and inventory, which are all in separate Git repositories.

I typically have a Tmux session called ansible with the following windows:

[ansible] 0:playbooks* 1:inventory  2:collections-

The playbooks window point to my playbook repository, inventory to ansible-inventory, and collection to /home/jiholland/ansible-collections/jiholland. That way I can easily switch between them.

In the playbook repo I gitignore certain files and directories that should not be pushed to Git.

$ cat playbooks/.gitignore
ansible.cfg
collections
roles

My inventory is split into development and production. The ansible.cfg file points to the development inventory, since I use AWX/AAP to run against the production environment. This file also contains other user-specific settings, which is why it is not pushed to Git.

$ cat playbooks/ansible.cfg
[defaults]
inventory=/home/jiholland/ansible-inventory/development

When it comes to AWX/AAP I add both the inventory and playbook repositories as two separate projects. I install collections and roles when building the execution environment, but an alternative approach is to have AWX/AAP install collections from a requirements.yml file:

$ tree -a playbooks/
playbooks/
└── collections
    ├── ansible_collections -> ../../ansible_collections
    └── requirements.yml

$ cat playbooks/.gitignore
collections/ansible_collections

Posted

in

by

Tags:

Comments

Leave a Reply

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