# Getting variables from a host script to use with playbooks

**URL:** <https://forum.ansible.com/t/getting-variables-from-a-host-script-to-use-with-playbooks/13771>\
**Category:** Ansible Project\
**Created:** [October 25, 2013, 5:56pm UTC](https://forum.ansible.com/t/getting-variables-from-a-host-script-to-use-with-playbooks/13771 "2013-10-25T17:56:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Norman\_Kraft](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@Norman\_Kraft](https://forum.ansible.com/u/Norman_Kraft)\
**Post date:** [October 25, 2013, 5:56pm UTC](https://forum.ansible.com/t/getting-variables-from-a-host-script-to-use-with-playbooks/13771/1 "2013-10-25T17:56:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [October 25, 2013, 6:02pm UTC](https://forum.ansible.com/t/getting-variables-from-a-host-script-to-use-with-playbooks/13771/2 "2013-10-25T18:02:11Z")

</div>

Norman,

It seems to me you want an external inventory script: [http://www.ansibleworks.com/docs/intro\_dynamic\_inventory.html](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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [October 25, 2013, 6:03pm UTC](https://forum.ansible.com/t/getting-variables-from-a-host-script-to-use-with-playbooks/13771/3 "2013-10-25T18:03:10Z")

</div>

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

if it returns json all should be available for your plays

---

<div class="post-metadata">

**Author:** ![Norman\_Kraft](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@Norman\_Kraft](https://forum.ansible.com/u/Norman_Kraft)\
**Post date:** [October 25, 2013, 6:08pm UTC](https://forum.ansible.com/t/getting-variables-from-a-host-script-to-use-with-playbooks/13771/4 "2013-10-25T18:08:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Norman\_Kraft](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@Norman\_Kraft](https://forum.ansible.com/u/Norman_Kraft)\
**Post date:** [October 25, 2013, 6:10pm UTC](https://forum.ansible.com/t/getting-variables-from-a-host-script-to-use-with-playbooks/13771/5 "2013-10-25T18:10:07Z")

</div>

Brain,

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