using host_vars in dynamically created inventory

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:

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 :slight_smile:

Everyone Advertises

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.

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

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.

Ah ok, great. Thanks Matt :slight_smile:

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.

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 but I’m not having much success understanding how to do it properly