On Fri, Jul 25, 2014 at 9:56 AM, Josh Smift <[email protected]> wrote: > 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. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgz9sEtuR%2BjX_4%2BkBryjq8nCwAmmWpud-xyBWUtqE_NN3g%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
