loop with dynamic item.x

32 views
Skip to first unread message

Guillaume D

unread,
Apr 1, 2019, 2:21:08 PM4/1/19
to Ansible Project
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

Kai Stian Olstad

unread,
Apr 1, 2019, 2:25:34 PM4/1/19
to ansible...@googlegroups.com
On 01.04.2019 20:21, Guillaume D wrote:
> 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 }}"

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

{{ lookup('vars', item['environnement'] }}


--
Kai Stian Olstad

James Cassell

unread,
Apr 1, 2019, 2:37:10 PM4/1/19
to Ansible List


On Mon, Apr 1, 2019, at 2:21 PM, Guillaume D wrote:
> 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 }} "

msg: "{{ item[environnement] }} "


V/r,
James Cassell

Guillaume D

unread,
Apr 1, 2019, 2:46:21 PM4/1/19
to Ansible Project
Thank you a lot guys, this is working as expected !
Reply all
Reply to author
Forward
0 new messages