Unified inventory

Our inventory comes from multiple sources, a couple dynamic and one
static. Ansible itself can pull them all together into a unified
inventory, but we'd like to transform the dynamic sources on the way in,
without having to hack the scripts that produce them, so we can use the
stock versions from .../plugins/inventory. For example, we might want to
set some variables based on what groups a host is in, add it to some
additional groups (e.g. if a host is in 'prod' and 'webserver', also add
it to 'prod-webservers'), etc.

I started out by writing a Python script which would take the output of
those scripts, json.loads() it into a big data structure, and then create
a new data structure that I could json.dumps() for Ansible to consume.
Along the way I ran into 'import ansible.inventory', which seemed like it
might be more appealing (and more Pythonic): Rather than slogging through
these giant data structures, perhaps I'd like to create an empty
ansible.inventory.Inventory object, and populate it with the information
from the dynamic and static sources.

I had two questions about that:

(1) Does this seem like a reasonable approach?

(2) If so, is there an easy way to dump the contents of an
    ansible.inventory.Inventory object into the JSON that Ansible itself
    wants to consume? I didn't see an obvious method for that, but might
    have missed something.

Thanks in advance for any advice and input.

                                      -Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

(1) Does this seem like a reasonable approach?

​Seems reasonable yes.
I'd actually love to see a framework for such things. Shouldn't even be
ansible specific.

(2) If so, is there an easy way to dump the contents of an
    ansible.inventory.Inventory object into the JSON that Ansible itself
    wants to consume? I didn't see an obvious method for that, but might
    have missed something.

​nothing in ansible core.
I have been hacking on a script ​that does just that, and I updated it to
the inventory refactor of past weeks, but atm it's not bug free yet, i need
to give it some more love:

https://github.com/ginsys/ansible-plugins/blob/devel/bin/ansible-inventory

Could also be used to generate a cached json file.

  Serge

Our inventory comes from multiple sources, a couple dynamic and one
static. Ansible itself can pull them all together into a unified
inventory, but we'd like to transform the dynamic sources on the way in,
without having to hack the scripts that produce them, so we can use the
stock versions from .../plugins/inventory. For example, we might want to
set some variables based on what groups a host is in, add it to some
additional groups (e.g. if a host is in 'prod' and 'webserver', also add
it to 'prod-webservers'), etc.

This sounds like you are on the verge of writing your own inventory script
or CMDB system. That's ok. That option is there for when things get too
site-specific to have a generic set of solutions.

I started out by writing a Python script which would take the output of
those scripts, json.loads() it into a big data structure, and then create
a new data structure that I could json.dumps() for Ansible to consume.
Along the way I ran into 'import ansible.inventory', which seemed like it
might be more appealing (and more Pythonic): Rather than slogging through
these giant data structures, perhaps I'd like to create an empty
ansible.inventory.Inventory object, and populate it with the information
from the dynamic and static sources.

I had two questions about that:

(1) Does this seem like a reasonable approach?

***maybe***.

I think I would be tempted to see if you could write an inventory script
but you may need to "build" the inventory.

Tower is a good place to store such a built inventory if you would want to
use the REST API to store various groups, which may also be a more
efficient route. You could then stop maintaining things in text files.

Only saying this as you are a user, generally this isn't too much of a
topic for this list unless its' a really good fit for a use case. Should
this be interesting, do be sure to use your support connections and we'd be
glad to share more info.

(2) If so, is there an easy way to dump the contents of an
    ansible.inventory.Inventory object into the JSON that Ansible itself
    wants to consume? I didn't see an obvious method for that, but might
    have missed something.

Ansible's inventory trees are mainly an internal API.