# \#loop

**URL:** https://forum.ansible.com/tag/loop/206.md

[Latest](https://forum.ansible.com/latest.md) · [Categories](https://forum.ansible.com/categories.md) · [Tags](https://forum.ansible.com/tags.md)

---

## [Breaking out of a loop early](https://forum.ansible.com/t/breaking-out-of-a-loop-early/46139)

<div class="topic-metadata">

**Author:** [@Toni](https://forum.ansible.com/u/Toni)\
**Replies:** 2\
**Last updated:** [August 7, 2026, 9:42am UTC](https://forum.ansible.com/t/breaking-out-of-a-loop-early/46139 "2026-08-07T09:42:43Z")

</div>

I want to download a file from a set of URLs in a loop, stopping after the first URL that actually has the file. Here’s a sample playbook illustrating the problem: --- - hosts: localhost gather\_facts: false tasks: …

---

## [\[Solved\] Create set\_fact list with loop not working](https://forum.ansible.com/t/solved-create-set-fact-list-with-loop-not-working/44892)

<div class="topic-metadata">

**Author:** [@Patrick](https://forum.ansible.com/u/Patrick)\
**Replies:** 1\
**Last updated:** [November 24, 2025, 9:38pm UTC](https://forum.ansible.com/t/solved-create-set-fact-list-with-loop-not-working/44892 "2025-11-24T21:38:11Z")

</div>

Hi, I am trying to create with set\_fact a list of facts with loop but I can’t get it to work. Can anyone please give me a hint what I’m doing wrong? I want a simple list so I can use it with ‘when’. The zones.results is…

---

## [How to loop over "include\_role" with a list of variable dictionaries?](https://forum.ansible.com/t/how-to-loop-over-include-role-with-a-list-of-variable-dictionaries/44625)

<div class="topic-metadata">

**Author:** [@Hoeze](https://forum.ansible.com/u/Hoeze)\
**Replies:** 5\
**Last updated:** [October 10, 2025, 8:43pm UTC](https://forum.ansible.com/t/how-to-loop-over-include-role-with-a-list-of-variable-dictionaries/44625 "2025-10-10T20:43:51Z")

</div>

Hi all, I am trying to loop over my “create\_user” role with a list of “users”: - name: Setup users include\_role: name: create\_user vars: "{{ user\_vars }}" loop: "{{ users\_to\_create }}" …

---

## [Help In Understanding/Resolving Loop Variables Within include\_tasks Loops Error](https://forum.ansible.com/t/help-in-understanding-resolving-loop-variables-within-include-tasks-loops-error/44257)

<div class="topic-metadata">

**Author:** [@Dulux-Oz](https://forum.ansible.com/u/Dulux-Oz)\
**Replies:** 2\
**Last updated:** [August 8, 2025, 9:52am UTC](https://forum.ansible.com/t/help-in-understanding-resolving-loop-variables-within-include-tasks-loops-error/44257 "2025-08-08T09:52:20Z")

</div>

Hi All, I’m having trouble sorting out the below issue, and so I’m looking for a little help. I know (actually, I suspect) that its something simple I’ve missed, but after several hours of googling, etc, I can’t work it…

---

## [Ansible output jinja2 loop to a different files](https://forum.ansible.com/t/ansible-output-jinja2-loop-to-a-different-files/42264)

<div class="topic-metadata">

**Author:** [@Shiraz\_Kamal](https://forum.ansible.com/u/Shiraz_Kamal)\
**Replies:** 2\
**Last updated:** [April 30, 2025, 11:28am UTC](https://forum.ansible.com/t/ansible-output-jinja2-loop-to-a-different-files/42264 "2025-04-30T11:28:10Z")

</div>

I have the following in my vars file. aux: "SHR" cluster\_nodes: - { node\_name: sudora123a, node\_num: 1, inst\_num: "123A" } - { node\_name: sudora123b, node\_num: 2, inst\_num: "123B" } I am trying to loop t…

---

## [Aggregating data from complex hierarchal sources](https://forum.ansible.com/t/aggregating-data-from-complex-hierarchal-sources/41522)

<div class="topic-metadata">

**Author:** [@sirklezero](https://forum.ansible.com/u/sirklezero)\
**Replies:** 3\
**Last updated:** [April 6, 2025, 3:54am UTC](https://forum.ansible.com/t/aggregating-data-from-complex-hierarchal-sources/41522 "2025-04-06T03:54:13Z")

</div>

I’m developing an Ansible playbook to help me automate the management of several dozen zones in Bind using the Community nsupdate module https://docs.ansible.com/ansible/latest/collections/community/general/nsupdate\_modu…

---

## [Help looping over local directories](https://forum.ansible.com/t/help-looping-over-local-directories/40636)

<div class="topic-metadata">

**Author:** [@vinylen](https://forum.ansible.com/u/vinylen)\
**Replies:** 1\
**Last updated:** [February 26, 2025, 1:10pm UTC](https://forum.ansible.com/t/help-looping-over-local-directories/40636 "2025-02-26T13:10:43Z")

</div>

Hello! Our company is moving to Ansible from SaltStack and i’ve been tearing my hair out over this one thing that i cannot really seem to figure out how to do! Basically this is the code: - name: Template example pipel…

---

## [Replacing value of list elements with info from another variable](https://forum.ansible.com/t/replacing-value-of-list-elements-with-info-from-another-variable/39256)

<div class="topic-metadata">

**Author:** [@JethCalark](https://forum.ansible.com/u/JethCalark)\
**Replies:** 3\
**Last updated:** [December 26, 2024, 9:30am UTC](https://forum.ansible.com/t/replacing-value-of-list-elements-with-info-from-another-variable/39256 "2024-12-26T09:30:58Z")

</div>

Working on a role for Authentik; as with any IdP, I’m dealing with users and groups. Authentik gives an idempotent API path to use for updating groups, and it uses JSON for the body of its API calls, which is convenient,…

---

## [Win\_shell + loops + delegate\_to does not work (not at all or as expected)](https://forum.ansible.com/t/win-shell-loops-delegate-to-does-not-work-not-at-all-or-as-expected/11203)

<div class="topic-metadata">

**Author:** [@msperl](https://forum.ansible.com/u/msperl)\
**Replies:** 0\
**Last updated:** [November 28, 2024, 10:33am UTC](https://forum.ansible.com/t/win-shell-loops-delegate-to-does-not-work-not-at-all-or-as-expected/11203 "2024-11-28T10:33:39Z")

</div>

I am trying to run several win\_shell command to different windows hosts for a single linux server. The number of iterations we want to run is totally defined by the inventory (i.e group membership). This unfortunately …

---

## [Need To Create List Of Dictionary With New Field 'device\_id' Added](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655)

<div class="topic-metadata">

**Author:** [@javed\_khan\_siddque](https://forum.ansible.com/u/javed_khan_siddque)\
**Replies:** 7\
**Last updated:** [November 25, 2024, 12:52pm UTC](https://forum.ansible.com/t/need-to-create-list-of-dictionary-with-new-field-device-id-added/9655 "2024-11-25T12:52:34Z")

</div>

I have my code written like below . But i am not able to get proper List of dictionary. --- - name: Create final list by adding device\_id to san\_mount\_list without exceeding size\_gb hosts: localhost gather\_facts: f…

---

## [Unexpected behavior change in loop over debug task in 2.14.2 -\> 2.16 upgrade](https://forum.ansible.com/t/unexpected-behavior-change-in-loop-over-debug-task-in-2-14-2-2-16-upgrade/6833)

<div class="topic-metadata">

**Author:** [@john.elion](https://forum.ansible.com/u/john.elion)\
**Replies:** 2\
**Last updated:** [June 28, 2024, 4:30pm UTC](https://forum.ansible.com/t/unexpected-behavior-change-in-loop-over-debug-task-in-2-14-2-2-16-upgrade/6833 "2024-06-28T16:30:43Z")

</div>

My development team has used “complex variables” (i.e., references to specific dictionary fields) in loops over the debug task for a long time. Example: - debug: var: "{{ item }}" with\_items: - result.rc - r…

---

## [How to execute a loop of actions on differents hosts?](https://forum.ansible.com/t/how-to-execute-a-loop-of-actions-on-differents-hosts/4522)

<div class="topic-metadata">

**Author:** [@romainquere](https://forum.ansible.com/u/romainquere)\
**Replies:** 8\
**Last updated:** [March 27, 2024, 1:25am UTC](https://forum.ansible.com/t/how-to-execute-a-loop-of-actions-on-differents-hosts/4522 "2024-03-27T01:25:46Z")

</div>

I would like to loop on a set of tasks which are executed on different hosts. For example: action 1 on host A action 2 on host B action 3 on host C repeated for each element of my variable TestName

---

## [Conditional loop using hostvars](https://forum.ansible.com/t/conditional-loop-using-hostvars/4362)

<div class="topic-metadata">

**Author:** [@chtsalid](https://forum.ansible.com/u/chtsalid)\
**Replies:** 3\
**Last updated:** [March 14, 2024, 9:15am UTC](https://forum.ansible.com/t/conditional-loop-using-hostvars/4362 "2024-03-14T09:15:57Z")

</div>

Hi all, I am encountering a problem using a conditional loop with hostvars. I am getting an error, that the conditional is not correct. Here underneath is my module: set\_fact: hostlist: “{{ hostlist | default() + \[ite…

---

## [Changing the dictionary key names but not values of items in a loop](https://forum.ansible.com/t/changing-the-dictionary-key-names-but-not-values-of-items-in-a-loop/3434)

<div class="topic-metadata">

**Author:** [@JethCalark](https://forum.ansible.com/u/JethCalark)\
**Replies:** 8\
**Last updated:** [January 28, 2024, 11:12pm UTC](https://forum.ansible.com/t/changing-the-dictionary-key-names-but-not-values-of-items-in-a-loop/3434 "2024-01-28T23:12:45Z")

</div>

I have a role for Adguard Home with a task to add DHCP reservations. That task requires a list variable where each item has the same dictionary keys: - name: Add static leases on AdGuard Home ansible.builtin.uri: …

---

## [Add fstab options and then only remount filesystems that have been changed](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395)

<div class="topic-metadata">

**Author:** [@wapaloosa](https://forum.ansible.com/u/wapaloosa)\
**Replies:** 12\
**Last updated:** [January 24, 2024, 1:16pm UTC](https://forum.ansible.com/t/add-fstab-options-and-then-only-remount-filesystems-that-have-been-changed/3395 "2024-01-24T13:16:22Z")

</div>

This is my first post so please bear with me. I created a playbook that will loop through a list of mount points and if the mount points exist and correct options do not exist it will add the correct options. This part …

---

## [Run a role in loop for multiple Elastic Ips](https://forum.ansible.com/t/run-a-role-in-loop-for-multiple-elastic-ips/3342)

<div class="topic-metadata">

**Author:** [@Faizan](https://forum.ansible.com/u/Faizan)\
**Replies:** 5\
**Last updated:** [January 19, 2024, 7:39pm UTC](https://forum.ansible.com/t/run-a-role-in-loop-for-multiple-elastic-ips/3342 "2024-01-19T19:39:56Z")

</div>

I have written a global role to spin-up ec2 instance at the address: playbook/aws/spin-up-ec2/tasks/main.yaml and i am calling the role in a main file at the location playbook/servers/cluster/tasks/main.yaml to deploy t…

---

## [Loop over roles with a list](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147)

<div class="topic-metadata">

**Author:** [@poinck](https://forum.ansible.com/u/poinck)\
**Replies:** 5\
**Last updated:** [October 6, 2023, 8:47am UTC](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147 "2023-10-06T08:47:35Z")

</div>

Hi, I want to loop over a role with a var like that, but the loop below does not work, because it cannot find a definition for item. array: - name: "a name" path: "/path/" - name: "Do Role" roles: - role: my\_…
