Skip to content

Create an Ansible Project Structure#

Problem#

How do I structure my Ansible project?

Solution#

Starting from a versioned project directory:

git init myproject
cd myproject

Let's create a directory structure for an Ansible project that should look like this:

.
├── collections
├── inventories
│ ├─── group_vars
│ │ └─── all
│ └── host_vars
├── playbooks
├── roles
└── roles_galaxy
mkdir -p playbooks roles roles_galaxy collections
mkdir -p inventories/group_vars/all inventories/host_vars

As well as, a minimal ansible.cfg:

ansible.cfg
[defaults]
## If you have the processing power available (hint: you have!) and want to increase forks!
## This speeds up Ansible by increasing parallism
forks = 50

## Default the inventory to use, can be overwritten using -i
;inventory = ./inventories/demo

remote_user = ansible
gathering = smart

collections_path = collections
roles_path = roles_galaxy:roles

## Cache Facts for few seconds
fact_caching = jsonfile
fact_caching_timeout = 10
fact_caching_connection = .cache

interpreter_python = auto_silent

[diff]
# Enable always show diff
;always = true

[ssh_connection]
pipelining = true

## Use the following config to control reuse of ssh connections
## and make use of openssh newer option accept-new
ssh_args = -C -o ControlMaster=yes -o ControlPersist=60s -o StrictHostKeyChecking=accept-new
A requirements.txt for dependencies on our control hosts:

requirements.txt
ansible==x.y.z

A requirements.yml for dependencies to external roles:

requirements.yml
roles: []
collections: []

Explanation#

The Playbooks and Inventories directories can be named differently, e.g. Hosts for the inventory directory, or Plays instead of Playbooks.

However, we will possibly assume this structure in further recipies. We go into the meaning of roles_galaxy and roles in more detail in Galaxy Roles.

See also#