Variables from another server

128 views
Skip to first unread message

Anthony Sheetz

unread,
Jun 20, 2016, 3:04:53 PM6/20/16
to Ansible Project
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:

---
# Called like: ansible-playbook -i ./ec2.py --extra-vars "chaos=test" aws_playground.yaml
- name: Collect our facts on bc-util
  hosts: "tag_Name_{{ chaos }}_bc_util"
  tasks:
  - debug: var=ansible_all_ipv4_addresses
 
- name: Set up a bc-web server
  hosts: "tag_Name_{{ chaos }}_bc_web"
  become: yes
  become_user: root
  remote_user: admin
  tasks:
  - debug: msg="{{ hostvars }}"

Matthew Kettlewell

unread,
Jun 21, 2016, 6:56:28 AM6/21/16
to Ansible Project

Anthony Sheetz

unread,
Jun 21, 2016, 10:23:42 AM6/21/16
to Ansible Project
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.

Josh Smift

unread,
Jun 21, 2016, 10:39:31 AM6/21/16
to ansible...@googlegroups.com
AS> If I dump hostvars, I can see the information I want does exist, I
AS> 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 (j...@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.

Josh Smift

unread,
Jun 21, 2016, 10:42:11 AM6/21/16
to ansible...@googlegroups.com
http://docs.ansible.com/ansible/playbooks_variables.html has some more
docs about this too.

Anthony Sheetz

unread,
Jun 22, 2016, 10:39:46 AM6/22/16
to Ansible Project, j...@care.com
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.

Anthony Sheetz

unread,
Jun 22, 2016, 10:58:06 AM6/22/16
to Ansible Project, j...@care.com
Finally cracked it:

- debug: var=hostvars[groups.tag_Name_{{ chaos }}_bc_util[0]].ansible_all_ipv4_addresses[0]

Brian Coca

unread,
Jun 22, 2016, 11:12:43 AM6/22/16
to ansible...@googlegroups.com, Josh Smift
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]


----------
Brian Coca

Anthony Sheetz

unread,
Jun 22, 2016, 3:21:01 PM6/22/16
to Ansible Project, j...@care.com
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]

Anthony Sheetz

unread,
Jun 22, 2016, 3:31:45 PM6/22/16
to Ansible Project
To wrap this whole thread up with a nice bow for anyone following behind:

There are three problems:
1. You need information from another server to use in the current server you're configuring.
2. The other server you need information from is hosted in AWS.
3. 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:

---
# Called like: ansible-playbook -i ./ec2.py --extra-vars "chaos=test" aws_playground.yaml
- name: Collect our facts on util
  hosts: "tag_Name_{{ chaos }}_util"
  tasks: [ ]

- name: Set up a web server
  hosts: "tag_Name_{{ chaos }}_web"
  tasks:
   - debug: var=hostvars[groups['tag_Name_'+chaos+'_util'][0]].ansible_all_ipv4_addresses[0]

devpatel...@gmail.com

unread,
Jun 7, 2017, 9:34:40 AM6/7/17
to Ansible Project
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?

Anthony Sheetz

unread,
Jun 7, 2017, 9:37:49 AM6/7/17
to Ansible Project
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.
Reply all
Reply to author
Forward
0 new messages