# Use hosts from groups within variable files

**URL:** <https://forum.ansible.com/t/use-hosts-from-groups-within-variable-files/18530>\
**Category:** Ansible Developer\
**Created:** [June 17, 2015, 11:24am UTC](https://forum.ansible.com/t/use-hosts-from-groups-within-variable-files/18530 "2015-06-17T11:24:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amir\_Luzon](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@Amir\_Luzon](https://forum.ansible.com/u/Amir_Luzon)\
**Post date:** [June 17, 2015, 11:24am UTC](https://forum.ansible.com/t/use-hosts-from-groups-within-variable-files/18530/1 "2015-06-17T11:24:46Z")

</div>

is it possible to access a host of a group from within a variable file (like Ansible does in Templates?)

here is the what i am trying to do:

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [June 17, 2015, 1:20pm UTC](https://forum.ansible.com/t/use-hosts-from-groups-within-variable-files/18530/2 "2015-06-17T13:20:23Z")

</div>

That shouldn’t be a problem. This example lists the ip address of the first node in the “db” group, for instance:

- name: Print a variable from inventory (different hostgroup)  
hosts: all  
tasks:
- debug:  
msg: “{{ hostvars[groups[‘db’][0]][‘ansible\_default\_ipv4’][‘address’] }}”

---

<div class="post-metadata">

**Author:** ![Amir\_Luzon](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@Amir\_Luzon](https://forum.ansible.com/u/Amir_Luzon)\
**Post date:** [June 17, 2015, 1:44pm UTC](https://forum.ansible.com/t/use-hosts-from-groups-within-variable-files/18530/3 "2015-06-17T13:44:55Z")

</div>

thanks for the quick response.

the example you gave is using a Playbook, what i am talking about it using this syntax within a variables file

---

<div class="post-metadata">

**Author:** ![Amir\_Luzon](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@Amir\_Luzon](https://forum.ansible.com/u/Amir_Luzon)\
**Post date:** [June 17, 2015, 2:13pm UTC](https://forum.ansible.com/t/use-hosts-from-groups-within-variable-files/18530/4 "2015-06-17T14:13:49Z")

</div>

never mind was able to make it work:
