# loop with dynamic item.x

**URL:** <https://forum.ansible.com/t/loop-with-dynamic-item-x/30034>\
**Category:** Ansible Project\
**Created:** [April 1, 2019, 6:21pm UTC](https://forum.ansible.com/t/loop-with-dynamic-item-x/30034 "2019-04-01T18:21:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaume\_D](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@Guillaume\_D](https://forum.ansible.com/u/Guillaume_D)\
**Post date:** [April 1, 2019, 6:21pm UTC](https://forum.ansible.com/t/loop-with-dynamic-item-x/30034/1 "2019-04-01T18:21:08Z")

</div>

Hello,

I’m blocking on this all the weekend.

I’m trying to build dynamically a variable with “item” for browsing in a list.

`

- debug:  
msg: "{{ item.dev }} "  
loop: “{{ nginx }}”  
`

This is working well.

But I need to have the choice of the environment.

I tried this:

`

- debug:  
msg: "{{ lookup(‘vars’, ‘item.’ + environnement }} "  
loop: “{{ nginx }}”  
`

The output is:  
`

fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘item.dev’\n\nThe error appears to have been in ‘’: line 22, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n”}  
`

I tried a lot of thing, but I didn’t find the right one.

Any advice?

Thank,  
Guillaume

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 1, 2019, 6:25pm UTC](https://forum.ansible.com/t/loop-with-dynamic-item-x/30034/2 "2019-04-01T18:25:23Z")

</div>

Because of the quotes item is a sting not a variable.  
Correct syntax is

&nbsp;&nbsp;{{ lookup('vars', item['environnement'] }}

---

<div class="post-metadata">

**Author:** ![cassell](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cassell](https://forum.ansible.com/u/cassell)\
**Post date:** [April 1, 2019, 6:36pm UTC](https://forum.ansible.com/t/loop-with-dynamic-item-x/30034/3 "2019-04-01T18:36:58Z")

</div>

> Hello,
> 
> I'm blocking on this all the weekend.
> 
> I'm trying to build dynamically a variable with "item" for browsing in a  
> list.
> 
> &nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.dev }} "  
> &nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ nginx }}"
> 
> This is working well.
> 
> But I need to have the choice of the environment.
> 
> I tried this:
> 
> &nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ lookup('vars', 'item.' + environnement }} "

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item[environnement] }} "

V/r,  
James Cassell

---

<div class="post-metadata">

**Author:** ![Guillaume\_D](https://avatars.discourse-cdn.com/v4/letter/g/77aa72/32.png) [@Guillaume\_D](https://forum.ansible.com/u/Guillaume_D)\
**Post date:** [April 1, 2019, 6:46pm UTC](https://forum.ansible.com/t/loop-with-dynamic-item-x/30034/4 "2019-04-01T18:46:21Z")

</div>

Thank you a lot guys, this is working as expected !
