I need to access information discovered from another server for a template on the server I’m configuring.
We are using AWS, and I’m of course using ec2.py to discover my hosts. I first configure a utility server, and then configure my web servers. I tag the utility server with an appropriate Name, and then in my web server configuration playbook I include a bit to discover facts about the utility server using appropriate tags. How do I then reference the facts about the utility server in the templates for the web server?
If I dump hostvars, I can see the information I want does exist, I just can’t convince Google to show me an example to follow.
Here’s an example playbook which DOES NOT work, but does illustrate the context:
The challenge isn’t finding facts about the current server the playbook is running on. It’s using facts about another server in the current playbook. I’ve already collected the facts I need - I just need to know how to access facts from another server for use on the current server.
If I dump hostvars, I can see the information I want does exist, I
just can't convince Google to show me an example to follow.
In general, you want something like
hostvars['server1']['ansible_ssh_host']
for the ansible_ssh_host variable on the host 'server1'; or, if the target
hostname is in a variable called 'host', something like
hostvars[host]['ansible_ssh_host']
or whatever.
-Josh (jbs@care.com)
(apologies for the automatic corporate disclaimer that follows)
This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
http://docs.ansible.com/ansible/playbooks_variables.html has some more
docs about this too.
-Josh (jbs@care.com)
(apologies for the automatic corporate disclaimer that follows)
This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
OK, I get that the data I want can be found by accessing hostvars[hostname][foo].
However, the hostname I want is variable. That is, when the utility server is created by a different ansible script, my only access to it is with a tag, not a hostname.
That is prone to breakage as it relies on double interpolation which does not always run, this is probably what you want:
- debug: var=hostvars[groups[‘tag_Name_’ + chaos + ‘_bc_util’][0]].ansible_all_ipv4_addresses[0]
Thank you Brian. Turns out the extra spaces cause problems. This worked:
- debug: var=hostvars[groups[‘tag_Name_’+chaos+‘_bc_util’][0]].ansible_all_ipv4_addresses[0]
To wrap this whole thread up with a nice bow for anyone following behind:
There are three problems:
- You need information from another server to use in the current server you’re configuring.
- The other server you need information from is hosted in AWS.
- The other server you need to access information about has a variable name
The solution to 3 is to specify the variable in the command line using extra-vars like so: ansible-playbook -i ./ec2.py --extra-vars “chaos=test” aws_playground.yaml
The solution to 2 is to find the hostname you need from the hostvars’ groups variable. When combined with 3 above, you want something like: - debug: var=hostvars[groups[‘tag_Name_’+chaos+‘_util’][0]].ansible_all_ipv4_addresses[0]
The solution to 1 is to first collect facts from the other server before working on the server you’re configuring. Below is a playbook collecting all this in one place:
It looks like the below settings only works for one host only as the groups will find its fist member with index 0 [groups[‘tag_Name_’+chaos+‘_util’][0]]
I am trying to figure out if I can utilize ip address for all hosts from previous play. I tried the below without any success.
-
debug: var=hostvars[groups[‘tag_Name_etc’]].ansible_all_ipv4_addresses
-
debug: var=hostvars[groups[‘tag_Name_etc’][0]].ansible_all_ipv4_addresses
-
debug: var=hostvars[groups[‘tag_Name_etc’]].ansible_all_ipv4_addresses[0]
-
debug: var=hostvars[groups[‘tag_Name_etc’]]
Any thoughts?
In my case, I wanted only the first host, so it did exactly what I wanted. We have one util server which needed its ip added to several other servers. To do the reverse, you would have to iterate. I don’t have how to do that at hand, unfortunately.