Getting variables from a host script to use with playbooks

I’m relatively new to Ansible, and still working these things out.

I have a setup where a Python script on the calling host runs a script against a configuration database, and returns a JSON hash of values. These values are to be fed into the playbook, to be used with whatever work needs to be done.

So far, I’ve tried putting the script call into a register variable, but that tries to call the script on each remote, where it will fail. I need the script to run on the host, before the playbook runs, and make the variable data available to the runs. I’m sure this kind of thing is done all the time, but combing through the Ansible docs hasn’t yet uncovered a solution.

Is this something that should be put into host variables? If so, how do I insert complex data from an external script into host variables?

Norman,

It seems to me you want an external inventory script: http://www.ansibleworks.com/docs/intro_dynamic_inventory.html

The script would fetch values from the configuration database and automagically assign them to hosts/groups. Then you could use those values in the plays.

  • James

ansible-playbook -e "$(run script)"

if it returns json all should be available for your plays

Thanks for the link. I saw that page, but thought that was just about passing in hostname inventories. I’ll have another look and see what I can figure out. The examples hint at what can be done, but the page on developing your own dynamic inventory is a bit sparse.

Brain,

That seems to be just what I was looking for! Thanks.