Reading INI inventory file from S3

Hey all,

In my workflow at work, we us Ansible for managing dynamically provisioned Azure VMs after they’re provisioned. Currently our provisioning scripts setup the Azure VMs, and then generate an ansible inventory file, which we store in AWS S3. This way, any of the operations folks who want to run post provisioning playbooks, they go download the inventory file from AWS S3, and then run the playbooks using the regular syntax ansible-playbook a-playbook.yml -i path/to/downloaded/inventory/file.

We recently upgraded to Ansible 2.4, and was wondering, that given the new inventory plugin refactor, if it would be possible for me to write a plugin, that wraps the INI plugin, but does the automatic download of the inventory from the AWS S3 source. How would I go about doing it? Specifically…

  1. Can I write my custom inventory plugin in my lib/ directory, and Ansible will pick it up? Or is there another path where it looks for inventory plugins.
  2. Is there any documentation on writing a custom plugin, including the interface of expected method calls and return values?
  3. Bonus, does the current design allow me to “compose” (in the sense of functional programming) plugins over existing plugins?

So for example in my case, I would just add a wrapper plugin class (or maybe a child class of the INI plugin), and add the logic to download the inventory file. Specifically something that would work with the command line ansible-playbook my-playbook.yml -i s3://my-bucket/inventory/path.

Thanks in advance,
Archit

P.S. Note the basic idea I’m going for is not really an Inventory plugin,as much as writing an inventory lookup plugin.

You shouldn’t need a plugin for this. Use a script which downloads and prints the inventory to stdout as your inventory file.

For example:
ansible-playbook -i script.sh

Josiah, oh interesting. I tried it but I got an error “Failed to parse /my/path/to/script with script plugin: … Syntax error while loading YAML”.

Does it expect YAML/JSON output?

Had to look up the documentation. Looks like it expects json output: http://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html

Sorry about that. I guess it’s not a perfect fit for your use case (INI file in S3)

Or you can just 'mount' s3 as a 'local drive' and point at it for inventory.

If you still want to 'autodownload' you might want to use an inventory
plugin instead of an inventory script.

Brian,

Exactly! I was trying to cook up a custom inventory plugin , by composing the existing ones. So back to my original question, where can I get some docs for writing one, or is reading the source of existing ones is the best idea.

Thanks in advance,
Archit

Writing the docs on how to write them is 'on my list'....

@Brian, I can help you write them, if you can guide me to some initial info, or style of.

Otherwise I was thinking of doing a blog style article on this, one I code up my inventory plugin based on reading the source code.