# Extracting IP of specific EC2 instance from dynamic inventory outside of current hostvars

**URL:** <https://forum.ansible.com/t/extracting-ip-of-specific-ec2-instance-from-dynamic-inventory-outside-of-current-hostvars/20005>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [December 14, 2015, 6:39pm UTC](https://forum.ansible.com/t/extracting-ip-of-specific-ec2-instance-from-dynamic-inventory-outside-of-current-hostvars/20005 "2015-12-14T18:39:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandr\_Kurilin](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Alexandr\_Kurilin](https://forum.ansible.com/u/Alexandr_Kurilin)\
**Post date:** [December 14, 2015, 6:39pm UTC](https://forum.ansible.com/t/extracting-ip-of-specific-ec2-instance-from-dynamic-inventory-outside-of-current-hostvars/20005/1 "2015-12-14T18:39:28Z")

</div>

Hey folks,

Ansible 1.9.3 user here. I’m trying to figure out if there’s a way to obtain the IP of a specific instance from an ec2 dynamic inventory when that instance is NOT part of the current group of hosts being configured. I’m using ec2.py to work with AWS. The way I’m currently identifying an environment and a type of instance in a single AWS account is by the combination of the Environment and Role tags. The assumption is that I’m not hardcoding any of these IPs anywhere.

Let’s say I’m running my applications playbook and those roles need to know the IP of my master database for that specific environment. Right now the only way I can see to obtain that IP is to write a wrapper script around ansible-playbook where I call `ec2.py --list` and figure out the right intersection of tags. I then pass that IP into ansible-playbook as a global variable through --extra-vars.

Is there a different and less convoluted approach to this? I would be ok with something like (tag\_Role\_db\_master:&tag\_Environment\_dev)[0].private\_ip if that were at all available, but that doesn’t seem to be the case.

Please advise and thank you in advance.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 16, 2015, 2:33pm UTC](https://forum.ansible.com/t/extracting-ip-of-specific-ec2-instance-from-dynamic-inventory-outside-of-current-hostvars/20005/2 "2015-12-16T14:33:43Z")

</div>

you don't need the host to be in the current list targeted by play,  
you can use hostvars['hostanme']['varname'] to access info on ANY host  
in the inventory.

as per figuring out the correct hostname you can use  
`lookup('inventory\_hostname',  
'tag\_Role\_db\_master:&tag\_Environment\_dev')` or  
`groups['tag_Role_db_master']|intersection(groups['tag_Environment_dev'])`  
to get the lists you want (add [0] for first host in lists)

---

<div class="post-metadata">

**Author:** ![Alexandr\_Kurilin](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Alexandr\_Kurilin](https://forum.ansible.com/u/Alexandr_Kurilin)\
**Post date:** [December 16, 2015, 6:43pm UTC](https://forum.ansible.com/t/extracting-ip-of-specific-ec2-instance-from-dynamic-inventory-outside-of-current-hostvars/20005/3 "2015-12-16T18:43:34Z")

</div>

Thanks for clarifying! The lookup route didn’t work for me, but the groups approach seems to be doing the trick. Here’s what I’m getting from the lookups:

TASK: [debug var=“{{ lookup(‘inventory\_hostname’, ‘tag\_Role\_utilities:&tag\_Environment\_dev’) }}”] \*\*\*  
fatal: [172.31.57.124] =\> Failed to template var=“{{ lookup(‘inventory\_hostname’, ‘tag\_Role\_utilities:&tag\_Environment\_dev’) }}”: lookup plugin (inventory\_hostname) not found

TASK: [debug var=“{{ lookup(‘inventory\_hostnames’, ‘tag\_Role\_utilities:&tag\_Environment\_dev’) }}”] \*\*\*  
fatal: [172.31.57.124] =\> Failed to template var=“{{ lookup(‘inventory\_hostnames’, ‘tag\_Role\_utilities:&tag\_Environment\_dev’) }}”: inventory\_hostnames must be used as a loop. Example: “with\_inventory\_hostnames: ‘all’”

Cheers.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 16, 2015, 8:32pm UTC](https://forum.ansible.com/t/extracting-ip-of-specific-ec2-instance-from-dynamic-inventory-outside-of-current-hostvars/20005/4 "2015-12-16T20:32:21Z")

</div>

lookup should work in 2.0 and beyond, but they are both equivalent in the end.
