# Looping through a group's IP numbers

**URL:** <https://forum.ansible.com/t/looping-through-a-groups-ip-numbers/14700>\
**Category:** Ansible Project\
**Created:** [March 4, 2014, 9:09pm UTC](https://forum.ansible.com/t/looping-through-a-groups-ip-numbers/14700 "2014-03-04T21:09:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Mahemoff](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Michael\_Mahemoff](https://forum.ansible.com/u/Michael_Mahemoff)\
**Post date:** [March 4, 2014, 9:09pm UTC](https://forum.ansible.com/t/looping-through-a-groups-ip-numbers/14700/1 "2014-03-04T21:09:32Z")

</div>

I have a rule like this:

- name: Create user

mysql\_user: name=app  
host=={{ item }}  
…  
with\_items:

- localhost
- 127.0.0.1
- “{{ groups[‘webservers’] }}”

# -

How can I make the list include IP numbers of each webserver host. I know I can use ansible\_eth0.ipv4.address to map from hostname to IP, but I don’t know how to map a whole list to another list. And I can’t see any way to define a variable as the result of a loop.

---

<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:** [March 4, 2014, 9:16pm UTC](https://forum.ansible.com/t/looping-through-a-groups-ip-numbers/14700/2 "2014-03-04T21:16:15Z")

</div>

I would split your create user into 2 tasks. 1 for localhost and the other for web servers:

- name: Create localhost mysql users  
mysql\_user: name=app host=“{{ item }}”  
with\_items:

- localhost

- 127.0.0.1

- name: Create webservers mysql users  
mysql\_user: name=app host=“{{ hostvars[item][‘ansible\_eth0’][‘ipv4’][‘address’] }}”  
with\_items: groups[‘webservers’]

---

<div class="post-metadata">

**Author:** ![Michael\_Mahemoff](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Michael\_Mahemoff](https://forum.ansible.com/u/Michael_Mahemoff)\
**Post date:** [March 5, 2014, 11:27am UTC](https://forum.ansible.com/t/looping-through-a-groups-ip-numbers/14700/3 "2014-03-05T11:27:37Z")

</div>

I see, thanks. I’d still be interested if there’s any other way to do it, as it would be useful elsewhere too and prefer not to duplicate plays if possible.

---

<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:** [March 5, 2014, 1:22pm UTC](https://forum.ansible.com/t/looping-through-a-groups-ip-numbers/14700/4 "2014-03-05T13:22:26Z")

</div>

The other way to do it would be something like:

- name: Create mysql servers  
mysql\_user: name=app host=“{{ hostvars[item][‘ansible\_eth0’][‘ipv4’][‘address’] if hostvars[item][‘ansible\_eth0’] is defined else item }}”  
with\_items:
- localhost
- 127.0.0.1
- "{{ groups[‘webservers’] }}

The above example should work with 1.5, although I do believe that is over complicated, hard to read and not easily understand.

You could expand that to a regular {% if … %}{{ address }}{% else %}{{ item }}{% endif %} type line, but that gets even longer and harder to read in my opinion with all of the special characters that get mixed into that line.
