Ansible: specify inventory file inside playbook

Question:

So from what I gather, we can use ansible.cfg to set the default inventory file that ansible looks for when running a playbook.

We can also override that inventory by using the -i parameter when running an ansible playbook via the command line.

Is there a way for me to specify a specific special inventory file inside a playbook without using the command line or changing the ansible.cfg file?

Answer:

You can form a dynamic inventory from inside your playbook using add_host module.
But you will have to write some inventory files parsers to feed correct parameters to add_host.

In general this is the wrong way to go. You should have playbooks and inventories separated.

Leave a Reply