# "register" list of ansible hostnames that are part of an inventory group

**URL:** <https://forum.ansible.com/t/register-list-of-ansible-hostnames-that-are-part-of-an-inventory-group/14302>\
**Category:** Ansible Project\
**Created:** [January 9, 2014, 6:07pm UTC](https://forum.ansible.com/t/register-list-of-ansible-hostnames-that-are-part-of-an-inventory-group/14302 "2014-01-09T18:07:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregory\_Spranger](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Gregory\_Spranger](https://forum.ansible.com/u/Gregory_Spranger)\
**Post date:** [January 9, 2014, 6:07pm UTC](https://forum.ansible.com/t/register-list-of-ansible-hostnames-that-are-part-of-an-inventory-group/14302/1 "2014-01-09T18:07:27Z")

</div>

hi all,

i am trying to be DRY, and wondering if i can somehow register all of the hosts that are part of inventory group .. for example:

#inventory  
[db\_slave]  
host1  
host2

#playbook

- name: create slave user for each slave host  
mysql\_user:  
name=slave  
password=foo  
host={{ item }}  
state=present  
with\_items:
- #need help here
- #somehow get list of hosts from group db\_slave

yes, i did see this example online:

{% for host in groups[‘db\_slave’] %}  
{{ hostvars[host][‘hostname’] }}  
{% endfor %}

and i even tried to put that into my roles “vars” directory .. but just kept getting syntax errors which makes sense ..

# slave hosts

mysql\_slave\_hosts:  
{% for host in groups[‘db\_slave’] %}

- {{ hostvars[host][‘hostname’] }}  
{% endfor %}

any thoughts ??

thanks as always,  
greg

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 9, 2014, 6:31pm UTC](https://forum.ansible.com/t/register-list-of-ansible-hostnames-that-are-part-of-an-inventory-group/14302/2 "2014-01-09T18:31:11Z")

</div>

I think what you probably want is something like:

#inventory  
[db\_slave]  
host1  
host2

#playbook

- name: create slave user for each slave host  
mysql\_user:  
name=slave  
password=foo  
host={{ hostvars[item][‘ansible\_hostname’] }}  
state=present  
with\_items: groups[‘db\_slave’]

No need to create another variable anywhere. And I used ansible\_hostname here, since hostname is not a standard var and I wasn’t sure if that is a custom var you have set for those hosts.

---

<div class="post-metadata">

**Author:** ![Gregory\_Spranger](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Gregory\_Spranger](https://forum.ansible.com/u/Gregory_Spranger)\
**Post date:** [January 9, 2014, 6:34pm UTC](https://forum.ansible.com/t/register-list-of-ansible-hostnames-that-are-part-of-an-inventory-group/14302/3 "2014-01-09T18:34:06Z")

</div>

awesome !!

didn't you know you could reference it directly .. thanks for the info  
and speedy reply -- will give it a try here in a bit ..

greg
