Ansible: Recommended directory structure and using variables

This post shows about a directory structure and using variables of Ansible I recommend from the aspect of management and operation.

I’ll talk about an example of management of some targets and groups those belong like the following figure.

Directory structure

Directory structure I recommend is the following.

Create a directory per each project, create ansible.cfg and inventory file on each project directory.

Comments

ansible.cfg

An ansible.cfg is a configuration file of Ansible. It can be on some directories those are the following from high to low priority. The highest existing file is applicable.
 1. Defined path by Environment variable ANSIBLE_CONFIG (if defined)
 2. ansible.cfg on the directory where ansible command is executed.
 3. /home/<user>/.ansible.cfg
 4. /etc/ansible/ansible.cfg
 5. Defined by Ansible as default
Copy /etc/ansible/ansible.cfg to each project directory and apply No.2 above.

If ansible-core 2.12 or later, run the following command to create a template of ansible.cfg.

ansible-config init --disabled > ansible.cfg

The following command can also create a more complete ansible.cfg that includes existing plugins.

ansible-config init --disabled -t all > ansible.cfg

ansible.cfg

 ・
 ・
 ・
[default]
inventory = inventory
 ・
 ・
 ・

inventory

An inventory file location is defined in ansible.cfg.
In this file, targets and groups those belong are defined like the following.
Variables used by playbooks can be defined in this file but to be defined in Host Variables and Group Variables not an inventory file is better from the aspect of operation and maintenance.

inventory

[group01]
192.168.1.1
192.168.1.2

[group02]
192.168.1.3
192.168.1.4

group_vars/<group name>

Group Variables are defined in group_vars/<group name> file and applicable on only targets belong to the group.
The name of directory group_vars is fixed.

host_vars/<hostname or IP address>

Host Variables are defined in host_vars/<hostname or IP address> file and applicable on the target.
The name of directory host_vars is fixed.

test_playbook.yml

A Playbook executable in this project.
Variables defined in group_vars and host_vars can be used in this playbook. Variables can be defined in playbook but not recommended.

Information to connect to Targets

Information to connect to Windows Server targets is defined in host_vars or group_vars file like the following.

ansible_user: Administrator
ansible_password: password
ansible_connection: winrm
ansible_winrm_transport: credssp
ansible_winrm_server_cert_validation: ignore

For the details, refer to the official website.
>> https://docs.ansible.com/ansible/2.9/user_guide/windows_winrm.html#authentication-options

Lastly

Because maintenance is to be easier, to create an inventory file per each project, create an ansible.cfg per each project, too.
And it is also easy to maintain, define variables as Group Variables or Host Variables not in playbook and inventory.