# Loops and includes

**URL:** <https://forum.ansible.com/t/loops-and-includes/21946>\
**Category:** Ansible Project\
**Created:** [July 7, 2016, 8:04am UTC](https://forum.ansible.com/t/loops-and-includes/21946 "2016-07-07T08:04:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [July 7, 2016, 8:04am UTC](https://forum.ansible.com/t/loops-and-includes/21946/1 "2016-07-07T08:04:03Z")

</div>

What is the difference between the following syntaxes?

=== 1st scenario

outside.yaml:

```auto
---
- include: inside.yaml
  vars:
    my_inner_item: "{{ item }}"
  with_items: 
    - A
    - B
    - C
 

```

inside.yaml:

```auto
---
- debug: msg="{{my_inner_item}} with {{ item }} is defined"
  with_items:
    - 1
    - 2
    - 3

```

=== 2nd scenario

outside.yaml:

```auto
---
- include: inside.yaml
with_items: 
    - A
    - B
    - C

```

inside.yaml:

```auto
---
- set_fact
my_inner_item={{item}}

- debug: msg="{{my_inner_item}} with {{ item }} is defined"
  with_items:
    - 1
    - 2
    - 3

```

=== 3rd Scenario:

outside.yaml:

```auto
---
- include: inside.yaml my_inner_item={{item}}
  with_items: 
    - A
    - B
    - C

```

inside.yaml:

```auto
---
- debug: msg="{{my_inner_item}} with {{ item }} is defined"
  with_items:
    - 1
    - 2
    - 3

```

… I do not list the old “loop\_control” scenario…

By the way: there is an existing issue connected to this topic:  
[https://github.com/ansible/ansible/issues/14146](https://github.com/ansible/ansible/issues/14146)

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [July 7, 2016, 11:59am UTC](https://forum.ansible.com/t/loops-and-includes/21946/2 "2016-07-07T11:59:15Z")

</div>

I think I messed up something 🙂  
This way my examples are more easy to understand:

=== 1st scenario

outside.yaml:

```auto
---
- include: inside.yaml
  vars:
    my_outer_item: "{{ item }}"
  with_items: 
    - A
    - B
    - C
 

```

inside.yaml:

```auto
---
- debug: msg="{{my_outer_item}} with {{ item }} is defined"
  with_items:
    - 1
    - 2
    - 3

```

=== 2nd scenario

outside.yaml:

```auto
---
- include: inside.yaml
with_items: 
    - A
    - B
    - C

```

inside.yaml:

```auto
---
- set_fact
my_outer_item={{item}}

- debug: msg="{{my_outer_item}} with {{ item }} is defined"
  with_items:
    - 1
    - 2
    - 3

```

=== 3rd Scenario:

outside.yaml:

```auto
---
- include: inside.yaml my_outer_item={{item}}
  with_items: 
    - A
    - B
    - C

```

inside.yaml:

```auto
---
- debug: msg="{{my_outer_item}} with {{ item }} is defined"
  with_items:
    - 1
    - 2
    - 3

```

Sources:  
[http://docs.ansible.com/ansible/playbooks\_loops.html#loops-and-includes-in-2-0](http://docs.ansible.com/ansible/playbooks_loops.html#loops-and-includes-in-2-0)  
[http://stackoverflow.com/questions/30785281/one-loop-over-multiple-ansible-tasks](http://stackoverflow.com/questions/30785281/one-loop-over-multiple-ansible-tasks)  
[https://github.com/ansible/ansible/issues/14146](https://github.com/ansible/ansible/issues/14146)

But it would be good to get some clarification about the differences.

1. július 7., csütörtök 10:04:03 UTC+2 időpontban Bence Takács a következőt írta:

---

<div class="post-metadata">

**Author:** ![Bence\_Takacs](https://avatars.discourse-cdn.com/v4/letter/b/c6cbf5/32.png) [@Bence\_Takacs](https://forum.ansible.com/u/Bence_Takacs)\
**Post date:** [July 8, 2016, 9:01am UTC](https://forum.ansible.com/t/loops-and-includes/21946/3 "2016-07-08T09:01:15Z")

</div>

Which one is the recommended way, and why?

1. július 7., csütörtök 13:59:15 UTC+2 időpontban Bence Takács a következőt írta:
