# using host\_vars in dynamically created inventory

**URL:** <https://forum.ansible.com/t/using-host-vars-in-dynamically-created-inventory/13628>\
**Category:** Ansible Project\
**Created:** [October 8, 2013, 10:00am UTC](https://forum.ansible.com/t/using-host-vars-in-dynamically-created-inventory/13628 "2013-10-08T10:00:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake\_Clarkson1](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@Jake\_Clarkson1](https://forum.ansible.com/u/Jake_Clarkson1)\
**Post date:** [October 8, 2013, 10:00am UTC](https://forum.ansible.com/t/using-host-vars-in-dynamically-created-inventory/13628/1 "2013-10-08T10:00:47Z")

</div>

Hey guys,

I’m using the Rackspace public cloud for hosting our app and as such I want to be able to create the Ansible hosts inventory file dynamically.

I’ve come across:

- [http://www.ansibleworks.com/docs/developing\_inventory.html](http://www.ansibleworks.com/docs/developing_inventory.html)

- [http://www.ansibleworks.com/docs/intro\_dynamic\_inventory.html](http://www.ansibleworks.com/docs/intro_dynamic_inventory.html)

The issue I’ve got is that I need to be able to include host\_vars in the JSON output of the script which creates the inventory dynamically. I need to do this so that I can specify `ansible_ssh_host` for the boxes. I can’t just use the IP because I also have associated files in host\_vars for the hosts.

e.g.

1. I have two clients: client01 and client02 (these are the cloud server names)

2. Each client has its own file in host\_vars/

3. client02 cloud server is destroyed and recreated, therefore IPv4 change

4. Hosts inventory needs to have the alias client02 but a value for ansible\_ssh\_host for its new IP

Thanks 🙂

![](http://www.digitalanimal.com/wp-content/uploads/2013/05/Unknown.png)

Everyone Advertises

[http://www.digitalanimal.com](http://www.digitalanimal.com)

**Digital Animal Limited is registered in England and Wales under company number: 07757607. Registered office 47 Castle Street Reading RG1 7SR. VAT No: GB 121 1672 57**

This electronic message contains information from Digital Animal Ltd which may be privileged or confidential. The information is intended to be for the use of the individuals or entity named above. If you are not the intended recipient be aware that any disclosure, copying, distribution or use of the contents of this information is prohibited. If you have received this electronic message in error, please notify us by telephone or email (to the numbers or address above) immediately.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [October 8, 2013, 11:55am UTC](https://forum.ansible.com/t/using-host-vars-in-dynamically-created-inventory/13628/2 "2013-10-08T11:55:42Z")

</div>

Jake,

There is a pull request open right now on the rax.py inventory script that addresses some of your issues.

[https://github.com/ansible/ansible/pull/4326](https://github.com/ansible/ansible/pull/4326)

It uses the name of the server in the inventory but sets ansible\_ssh\_host using the IP of the server as a hostvar in the output of the inventory script.

With that improvement, there should be no concern with using the name as a host\_var file. Just waiting on the merge to happen.

---

<div class="post-metadata">

**Author:** ![Jake\_Clarkson1](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@Jake\_Clarkson1](https://forum.ansible.com/u/Jake_Clarkson1)\
**Post date:** [October 8, 2013, 12:20pm UTC](https://forum.ansible.com/t/using-host-vars-in-dynamically-created-inventory/13628/3 "2013-10-08T12:20:19Z")

</div>

Ah ok, great. Thanks Matt 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 8, 2013, 3:06pm UTC](https://forum.ansible.com/t/using-host-vars-in-dynamically-created-inventory/13628/4 "2013-10-08T15:06:25Z")

</div>

FYI, you can still use group\_vars/ and host\_vars/ with dynamic inventory scripts.

Just put them alongside your scripts or playbooks just as you would do normally, and you can define ansible\_ssh\_host there if you want.

---

<div class="post-metadata">

**Author:** ![Christopher\_Laco](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@Christopher\_Laco](https://forum.ansible.com/u/Christopher_Laco)\
**Post date:** [October 16, 2013, 3:06pm UTC](https://forum.ansible.com/t/using-host-vars-in-dynamically-created-inventory/13628/5 "2013-10-16T15:06:18Z")

</div>

I’m in sort of the same boat. I don’t necessarily want dynamic inventory, but I just want to collect information from the rax tasks that I just ran in a playbook and drop those new servers into a templates inventory file.

Pretty much this: [http://comments.gmane.org/gmane.comp.sysutils.ansible/2700](http://comments.gmane.org/gmane.comp.sysutils.ansible/2700) but I’m not having much success understanding how to do it properly
