# How do I do looping on playbook?

**URL:** <https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052>\
**Category:** Ansible Project\
**Created:** [July 2, 2013, 7:22am UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052 "2013-07-02T07:22:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kialeo](https://avatars.discourse-cdn.com/v4/letter/k/22d042/32.png) [@kialeo](https://forum.ansible.com/u/kialeo)\
**Post date:** [July 2, 2013, 7:22am UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/1 "2013-07-02T07:22:55Z")

</div>

Hello, below is my play book

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 2, 2013, 6:36pm UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/2 "2013-07-02T18:36:37Z")

</div>

Look up “with\_items” in the documentation for examples for the looping construct in Ansible. You would then use the “item” variable to represent what is looped over in each cycle of the loop inside the task.

Also, I should point out that “only\_if” is rather dated in Ansible, look at the 1.2 documentation for how to use “when” and things get much simpler.

when: value == 'hello"

etc

---

<div class="post-metadata">

**Author:** ![kialeo](https://avatars.discourse-cdn.com/v4/letter/k/22d042/32.png) [@kialeo](https://forum.ansible.com/u/kialeo)\
**Post date:** [July 3, 2013, 1:42am UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/3 "2013-07-03T01:42:29Z")

</div>

Thanks Michael for the reply. Awesome. I tried this:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 3, 2013, 1:46am UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/4 "2013-07-03T01:46:25Z")

</div>

You can simplify:

with\_items:

- motd1
- motd2

Otherwise you’d have to do {{ item.item }}

the way you had it.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![kialeo](https://avatars.discourse-cdn.com/v4/letter/k/22d042/32.png) [@kialeo](https://forum.ansible.com/u/kialeo)\
**Post date:** [July 3, 2013, 2:32am UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/5 "2013-07-03T02:32:34Z")

</div>

Hi, I simplified my declaration. Unfortunately, i still got the same error. ERROR: /home/me/ Documents/${item} does not exist.

can lookup read ${item}? Seems like it cannot.

And is there other way to do iteration?

with\_item is good for small number of variables eg. motd1 to motd5. However, if i want motd1 to motd1000, i will have to have with\_items 1 to 1000? is it possible to have a variable i and loop it iteratively? eg motd[i]

Thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 3, 2013, 3:00am UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/6 "2013-07-03T03:00:27Z")

</div>

Change your lookup call to look like this…

value: “{{ lookup(‘file’,‘/home/me/Documents/’ + item) }}”

${foo} is old school stuff and shouldn’t be mixed with {{ foo }}

---

<div class="post-metadata">

**Author:** ![GW1](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@GW1](https://forum.ansible.com/u/GW1)\
**Post date:** [July 3, 2013, 3:18pm UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/7 "2013-07-03T15:18:51Z")

</div>

Hi,

Despite other problems, the biggest is that you are trying to iterate in the _vars_ section! You can only iterate in tasks (or handlers).

Probably something like:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 3, 2013, 3:22pm UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/8 "2013-07-03T15:22:41Z")

</div>

you are doing with\_items at the play level, it is normally done at the task level

hosts: all

user: xyz

tasks:

- action: copy src=/home/me/Documents/“{{lookup(‘file’,'/home/me/ Documents/”’ + item )}}" dest=/home/me2/Desktop/{{item}}

name: to copy

with\_items:

- ‘motd1’

- ‘motd2’

Brian Coca

---

<div class="post-metadata">

**Author:** ![celine](https://avatars.discourse-cdn.com/v4/letter/c/13edae/32.png) [@celine](https://forum.ansible.com/u/celine)\
**Post date:** [July 4, 2013, 1:35am UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/9 "2013-07-04T01:35:48Z")

</div>

> I am guessing he/she probably wants to loop all the tasks, not jus that task itself. But reiterate all the tasks in that playbook. Can that be done?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 4, 2013, 4:17am UTC](https://forum.ansible.com/t/how-do-i-do-looping-on-playbook/13052/10 "2013-07-04T04:17:56Z")

</div>

There is an unsupported way to include multiple tasks based on a loop, but it’s not supported because it’s hyper-confusing and people try to do it with inventory variables.

It is preferred to loop on individual tasks only.
