1 min to read
Splitting your Ansible Hosts file
Split hosts file by client

Instead of having a single monolithic ansible hosts file you can split them up by client.
Previously you would have:
ansible/hosts
ansible/host_vars
ansible/host_vars/client_1.com.yml
ansible/host_vars/client_2.com.yml
ansible/group_vars
ansible/group_vars/client_1_production.yml
ansible/group_vars/client_1_staging.yml
ansible/group_vars/client_2_production.yml
Where the hosts file would contain
[client_1_production]
client_1.com
[client_1_staging]
staging.client_1.com
[client_2_production]
client_2.com
Which gets difficult to manage when you have many clients and their infrastructure starts to bloat. A nice way to handle this is to split the ansible hosts using an inventories directory.
This allows you to create a directory structure like this:
ansible/inventories/client_1
ansible/inventories/client_1/hosts.ini
ansible/inventories/client_1/group_vars/client_1_production
ansible/inventories/client_1/group_vars/client_1_staging
ansible/inventories/client_1/host_vars/client_1_com.yml
ansible/inventories/client_2
ansible/inventories/client_2/hosts.ini
ansible/inventories/client_2/group_vars/client_1_production
ansible/inventories/client_2/host_vars/client_2_com.yml
With the hosts files split
> ansible/inventories/client_1/hosts.ini
[client_1_production]
client_1.com
[client_1_staging]
staging.client_1.com
> ansible/inventories/client_2/hosts.ini
[client_2_production]
client_2.com