Questions for "Ansible Alternative Directory Structure" with split inventories

Hello -

I have a use case where I have two of the same apps installed on one server in a shared Prod-Like/Disaster Recovery (PL/DR) environment. I use Ansible to automate application configuration tasks for each app on the server. As of right now, this isn’t working for PL/DR because Ansible has no way to determine which host_vars are required. Does it load PL or does it load DR?

I did some searching and found Ansible Best Practices - Alternative Directory Layout. It looks like this might solve my problem. It splits out a “production” and “staging” directory. Each has their own host, group, and inventory files.

My questions are are around how this works? What is the “glue” such that Ansible knows how to find certain files? In their example layout, how does the playbook “site.yml” know to load the ‘production’ inventory versus the ‘staging’ inventory? Is it all hard coded user facts? Does it assume we specify ‘-i’ each time?

Any info, other examples, or other potential solves for this would be much appreciated.

Thank you!

Default inventory file is usually /etc/ansible/hosts, although you can override this by setting something different in your ansible.cfg (along with a lot of other options - see http://docs.ansible.com/ansible/intro_configuration.html )

As well as using -i to specify which inventory you want to load you can also set an environment variable.

export ANSIBLE_INVENTORY=your_inventory_file

Hope this helps,

Jon

Hello - Thanks for the response.

Yea right now I have a top level ansible.cfg that already points to my own inventory file (currently just one).

I guess I’m still not connecting the dots as to how site.yml (in their example) would know which inventory, host_vars, and group_vars to load. Is there an ansible.cfg that also needs to be split into the ‘production’ and ‘staging’ directories?

If I used the environment variable option that you presented above, I’m assuming I would load that in the .profile for the user running the playbook? In that way my PL user and DR user would each point to their own inventory file (via the .profile when the shell is opened) even before the ansible playbook is executed. Also, I wouldn’t have to hard code the inventory into the playbook or the command line. Am I thinking about that right? Sorry for the perhaps simple question, I’m still getting used to Linux systems.

Would the host_vars and group_vars then come along via the same solve as the inventory file? Or are those separate environment variables? I thought the host_vars and group_vars, by default, look in the same top location as where the playbook is executed,?

Thanks for the help!

As I understood it the distinction is yours to make, e.g. calling
ansible-playbook with "-i production/hosts" or by setting the correct
environment variable. Ansible does not automagically know which group
you want to target.

Johannes

You don't need to create separate users. Environment variables can be set for a single shell session, or you can use Ansible's `-i` flag to specify the inventory per-run instead.

I have a fair bit of experience using multiple inventories in the same repo now. Here's the setup I'd recommend:

* Specify the default inventory you want to use in ansible.cfg (hostfile = path/to/prod/).
* Override the default when you're configuring the DR environment (ansible-playbook playbook.yml -i path/to/pldr/).

Another strategy I've used for per-inventory options: set the correct variables in a file in your Ansible repo, and source that in your shell when you want to work on that environment, e.g.:

# /path/to/ansible/repo/prodvars.sh
# For example let's say you need to set options for ec2.py:
AWS_REGION=us-west-2
AWS_PROFILE=profilename
# ...and the inventory or other Ansible params:
ANSIBLE_INVENTORY=path/to/prod/

(Note: in case the convention isn't clear, wherever it says `path/to/...` above, replace that with the real path.)

pacem in terris / мир / शान्ति / ‎‫سَلاَم‬ / 平和
Kevin R. Bullock

Thank you both for the responses! This was very helpful.

I ended up creating a ‘production’ and ‘staging’ sub directories just like the Ansible sample. I removed the overall ansible.cfg inventory location, and instead used the ‘ANSIBLE_INVENTORY’ environment variable in the ,profile for each account. In my deployment, we already have separate accounts for PL vs DR so it worked out quite well. In this fashion, PL and DR can properly load their own host_vars without requiring and additional command line arguments. There is just some additional first time setup config to accommodate this.

Thank you!