Multiple simultaneous inventory sources

I've just merged a very nice patch set from @dhozac that enables the
inventory file to be a directory

If it is a directory, it operates much like a traditional "conf.d"
structure, except it allows both inventory files and
inventory scripts to be comingled.

This means that you can use EC2 and physical-file-based inventory
together, and then specify a "-i" inside the
directory to specifically target one and not the other.

When using this, the patch is structured such that "group_vars" and
"host_vars" structures live *inside* the directory, so it's always
going to work whether you use the whole directory of inventroy sources
or just one file on the inside.

I know this was a much requested feature so this is great to see...
multiple dynamic inventory sources at once, conf.d files,
or both, at the same time in any combination you want!

So thanks to Daniel for this one. It will be up on the documentation
pages eventually.

Cool, thanks @dhozac!

+1{,}

This sounds pretty cool. I will have to dig into this one. Thanks!

Is there any support for specifying multiple specific inventory files. For example, I may have the following clusters of service machines.

ProdClusterInventory
StagingClusterInventory
QAClusterInventory
DevClusterInventory
CommonClusterInventory

Prod thru Dev all need to refer to database machine held in the CommonClusterInventory – so naturally, I often want playbooks to refer to machine groups in, say ,ProdClusterInventory and CommonClusterInventory.

ansible -i appears to only allow a single inventory file to be specified. Using -i to refer to a directory:

DevCluster/
DevInventory

CommonInventory

means that CommonInventory is duplicate many times. (And I can’t commit symlinks to our source control)

Ideally (I think), I should be able to say an inventory file includes another.

  • Stuart

I believe the recommended approach for you would be to instead use inventory scripts. At that point, your bounds are limitless.

http://docs.ansible.com/developing_inventory.html

An inventory script can use mostly anything as it’s data source. You just need to write the code to handle your scenario.

“means that CommonInventory is duplicate many times. (And I can’t commit symlinks to our source control)”

It sounds like you need to better arrange groups such that you can specify groups, rather than breaking them up into seperate inventories maybe.

Though for larger cases or if you want something more custom, inventory scripts are useful, and if using directories, you can even do things like speak dynamically to Rackspace and EC2 and GCE all in the same call.

“means that CommonInventory is duplicate many times. (And I can’t commit symlinks to our source control)”

It sounds like you need to better arrange groups such that you can specify groups, rather than breaking them up into seperate inventories maybe.

Possibly, the good advice in your best practices document is to separate production/staging/qa/dev …

I think this is essential for us.

Though for larger cases or if you want something more custom, inventory scripts are useful, and if using directories, you can even do things like speak dynamically to Rackspace and EC2 and GCE all in the same call.

Yes – looking at that route. Now, hacking ec.py to provide a limited view of all our ec2 instances.

Thanks,

  • Stuart

How does -i determine which files are included and which are not? Can I have edit backups (./aaa~), README.txt, etc… in that directory?

Also, are there any plans to allow multiple specific inventory files? (It looks like a common request – are problems anticipated with such a feature?

  • Stu

“How does -i determine which files are included and which are not? Can I have edit backups (./aaa~), README.txt, etc… in that directory?”

It ignores files ending in “.ini” as well as files ending in known backup extensions or files starting with dot (hidden files)

Take a look at dir.py in the inventory/ subtree of the source if you are really curious… but basically most backup extensions will be ignored.

Hi

It is possible to provide a list of files (not directory) to -i option ??

i have next use case:

inventories/amazon/ec2.py
inventories/gce/gce.py

inventories/inventory

ec2 and gce provide different group names, like:
tag_Name_stage for amazon

tag_stage for google

(or even more complex)

in inventory file i created

[tag_stage]
[tag_Name_stage]

[app_stage:children]
tag_Name_stage

tag_stage

and when in playbook i use hosts: “app_stage”

i want to put -i inventories/amazon/ec2.py , inventories/inventory
or
i want to put -i inventories/gce/gce.py , inventories/inventory

and not both

thanks…

no, but you can symlink those files(and their configs) into a
directory and provide that.

inventories/amazon/ec2.py
inventories/gce/gce.py
inventories/inventory

inventories/withgce/inventory
inventories/withgce/gce.py

inventories/withec2/inventory
inventories/withec2/ec2.py

Nice solution :slight_smile:
Why didn’t i think of that myself??