# Ansible dynamic looping

**URL:** <https://forum.ansible.com/t/ansible-dynamic-looping/26632>\
**Category:** Ansible Project\
**Created:** [February 2, 2018, 12:01am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632 "2018-02-02T00:01:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Morgan2](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Andrew\_Morgan2](https://forum.ansible.com/u/Andrew_Morgan2)\
**Post date:** [February 2, 2018, 12:01am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/1 "2018-02-02T00:01:51Z")

</div>

Hello all,

I have a play I want to run wich deletes all hosts matching a certain criteria:

`  
tasks:

- name: Delete unavailable nodes  
zabbix\_host:  
server\_url: [http://zabbix.aa.net/zabbix](http://zabbix.aa.net/zabbix)  
login\_user: Admin  
login\_password: aq!  
host\_groups: Discovered hosts  
status: disabled  
state: absent  
host\_name: “{{ item }}”  
with\_items:
- ip-10-1-_-_.us-west-2.compute.internal

`

The above doesnt work, but if I list out the items  
`with_items:`  
`- ip-10-1-65-27.us-west-2.compute.internal`  
`- ip-10-1-65-28.us-west-2.compute.internal`  
`- ip-10-1-65-37.us-west-2.compute.internal`

`then it works, but I want it to be dynamic and find all ips matching ``- ip-10-1-*-*.us-west-2.compute.internal`

any help please?  
``

---

<div class="post-metadata">

**Author:** ![Le\_M\_nh\_Hung](https://avatars.discourse-cdn.com/v4/letter/l/7cd45c/32.png) [@Le\_M\_nh\_Hung](https://forum.ansible.com/u/Le_M_nh_Hung)\
**Post date:** [February 2, 2018, 12:37am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/2 "2018-02-02T00:37:09Z")

</div>

Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:

---

<div class="post-metadata">

**Author:** ![Le\_M\_nh\_Hung](https://avatars.discourse-cdn.com/v4/letter/l/7cd45c/32.png) [@Le\_M\_nh\_Hung](https://forum.ansible.com/u/Le_M_nh_Hung)\
**Post date:** [February 2, 2018, 12:46am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/3 "2018-02-02T00:46:25Z")

</div>

Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:

> Hello all,
> 
> I have a play I want to run wich deletes all hosts matching a certain criteria:
> 
> tasks:  
> - name: Delete unavailable nodes  
> zabbix\_host:  
> server\_url: [http://zabbix.aa.net/zabbix](http://zabbix.aa.net/zabbix)  
> login\_user: Admin  
> login\_password: aq!  
> host\_groups: Discovered hosts  
> status: disabled  
> state: absent  
> host\_name: "{{ item }}"  
> with\_items:  
> - ip-10-1-\*-\*.us-west-2.compute.internal
> 
> The above doesnt work, but if I list out the items  
> with\_items:  
> - ip-10-1-65-27.us-west-2.compute.internal  
> - ip-10-1-65-28.us-west-2.compute.internal  
> - ip-10-1-65-37.us-west-2.compute.internal
> 
> then it works, but I want it to be dynamic and find all ips matching - ip-10-1-\*-\*.us-west-2.compute.internal
> 
> any help please?

Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:

> Hello all,
> 
> I have a play I want to run wich deletes all hosts matching a certain criteria:
> 
> tasks:  
> - name: Delete unavailable nodes  
> zabbix\_host:  
> server\_url: [http://zabbix.aa.net/zabbix](http://zabbix.aa.net/zabbix)  
> login\_user: Admin  
> login\_password: aq!  
> host\_groups: Discovered hosts  
> status: disabled  
> state: absent  
> host\_name: "{{ item }}"  
> with\_items:  
> - ip-10-1-\*-\*.us-west-2.compute.internal
> 
> The above doesnt work, but if I list out the items  
> with\_items:  
> - ip-10-1-65-27.us-west-2.compute.internal  
> - ip-10-1-65-28.us-west-2.compute.internal  
> - ip-10-1-65-37.us-west-2.compute.internal
> 
> then it works, but I want it to be dynamic and find all ips matching - ip-10-1-\*-\*.us-west-2.compute.internal
> 
> any help please?

Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [February 2, 2018, 2:08am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/4 "2018-02-02T02:08:27Z")

</div>

with\_items requires a list to iterate over. I think one way of achieving your outcome is to create a group in the inventory and populate it with the hosts and run the playbook against that group.

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [February 2, 2018, 5:38am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/5 "2018-02-02T05:38:55Z")

</div>

Thanks for your reply, but the issue with that is , the servers in aws get removed and created again each build( every 1hr) , so I would need a way to get the servers dynamically.Is there anyway to do that? i want to dynamically have a list, not create a list of servers in inventory.

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [February 2, 2018, 7:55am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/6 "2018-02-02T07:55:57Z")

</div>

Hi,

If you use the dynamic inventory script (ec2.py) you can use tags to dynamically create groups. Have a look here: [https://aws.amazon.com/blogs/apn/getting-started-with-ansible-and-dynamic-amazon-ec2-inventory-management/](https://aws.amazon.com/blogs/apn/getting-started-with-ansible-and-dynamic-amazon-ec2-inventory-management/)

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![Mark\_Lam](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Mark\_Lam](https://forum.ansible.com/u/Mark_Lam)\
**Post date:** [February 2, 2018, 8:02am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/7 "2018-02-02T08:02:12Z")

</div>

Hi,

can you try “with\_fileglob” ? Refer in [http://docs.ansible.com/ansible/latest/playbooks\_loops.html#id4](http://docs.ansible.com/ansible/latest/playbooks_loops.html#id4)

Vào 07:01:51 UTC+7 Thứ Sáu, ngày 02 tháng 2 năm 2018, Andrew Morgan đã viết:

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [February 3, 2018, 10:48pm UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/8 "2018-02-03T22:48:22Z")

</div>

thank you , with files worked, after piping list of ips

---

<div class="post-metadata">

**Author:** ![Mark\_Lam](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@Mark\_Lam](https://forum.ansible.com/u/Mark_Lam)\
**Post date:** [February 4, 2018, 3:20am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/9 "2018-02-04T03:20:30Z")

</div>

Hi Andrew

Can you show us how you did it?

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [February 4, 2018, 4:24am UTC](https://forum.ansible.com/t/ansible-dynamic-looping/26632/10 "2018-02-04T04:24:20Z")

</div>

I did it by

host\_name: “{ item }”  
with\_lines: cat “file”
