Defining complex data (cascaded arrays) in yaml has problems

14 views
Skip to first unread message

Ralf Weber

unread,
Mar 4, 2019, 10:07:52 AM3/4/19
to Ansible Project
Moin!

For a recent project I needed to have a data structure with nested
arrays, however I could not get it to work using yaml syntax. When using
the following playbook:

- hosts: all
tasks:
- name: Set some test facts
set_fact:
bla: True
stub: [["mystub.example.net", [["ns1.mystub.example.net",
["192.0.2.4"]]]]]
stub2:
-
- mystub2,example.net
-
- ns1.mystub2.example.net
-
- 192.0.2.5
- name: Debug stub
debug: var=stub
- name: Debug stub2
debug: var=stub2

The debug output looks like this:

TASK [Debug stub]
**********************************************************************************
ok: [127.0.0.1] => {
"stub": [
[
"mystub.example.net",
[
[
"ns1.mystub.example.net",
[
"192.0.2.4"
]
]
]
]
]
}

TASK [Debug stub2]
*********************************************************************************
ok: [127.0.0.1] => {
"stub2": [
[
"mystub2,example.net - - ns1.mystub2.example.net - -
192.0.2.5"
]
]
}

but IMHO these data structures should be identical. I also tried to put
the same yams in a var file with the same result. What am I doing wrong?
Any ideas?

So long
-Ralf
—--
Ralf Weber

Kai Stian Olstad

unread,
Mar 4, 2019, 10:55:25 AM3/4/19
to ansible...@googlegroups.com
On 04.03.2019 12:56, Ralf Weber wrote:
> Moin!
>
> For a recent project I needed to have a data structure with nested arrays, however I could not get it to work using yaml syntax. When using the following playbook:
>
> - hosts: all
>   tasks:
>     - name: Set some test facts
>       set_fact:
>         bla: True
>         stub: [["mystub.example.net", [["ns1.mystub.example.net", ["192.0.2.4"]]]]]
>         stub2:
>           -
>             - mystub2,example.net
>               -
>                 - ns1.mystub2.example.net
>                   -
>                     - 192.0.2.5

<snip />

>
> but IMHO these data structures should be identical. I also tried to put the same yams in a var file with the same result. What am I doing wrong? Any ideas?

The correct syntax for nested list is

stub2:
- - mystub2,example.net
- - - ns1.mystub2.example.net
- - 192.0.2.5

or

stub2:
-
- mystub2,example.net
-
-
- ns1.mystub2.example.net
-
- 192.0.2.5


--
Kai Stian Olstad

Ralf Weber

unread,
Mar 4, 2019, 11:46:54 AM3/4/19
to ansible...@googlegroups.com
Moin!

On 4 Mar 2019, at 16:55, Kai Stian Olstad wrote:
> The correct syntax for nested list is
>
> stub2:
> - - mystub2,example.net
> - - - ns1.mystub2.example.net
> - - 192.0.2.5
>
> or
>
> stub2:
> -
> - mystub2,example.net
> -
> -
> - ns1.mystub2.example.net
> -
> - 192.0.2.5

Thanks a lot. That was the trick I was looking for. You missed one array
( the structure is complex ;-). Fully correct the yams represantion is:

stub2:
-
- mystub2,example.net
-
-
- ns1.mystub2.example.net
-
- 192.0.2.
stub2:
- - mystub2,example.net
- - - - ns1.mystub2.example.net
- - 192.0.2.5

but that was easy to fix myself, so thanks again.

Kai Stian Olstad

unread,
Mar 4, 2019, 12:14:06 PM3/4/19
to ansible...@googlegroups.com
On 04.03.2019 17:44, Ralf Weber wrote:
> Moin!
>
> On 4 Mar 2019, at 16:55, Kai Stian Olstad wrote:
>> The correct syntax for nested list is
>>
>>   stub2:
>>     - - mystub2,example.net
>>       - - - ns1.mystub2.example.net
>>           - - 192.0.2.5
>>
>> or
>>
>>   stub2:
>>     -
>>       - mystub2,example.net
>>       -
>>         -
>>           - ns1.mystub2.example.net
>>           -
>>             - 192.0.2.5
>
> Thanks a lot. That was the trick I was looking for. You missed one array ( the structure is complex ;-).

I don't think I missed an array, but I didn't test it so I did now.

- hosts: localhost
gather_facts: false
vars:
stub: [["mystub.example.net", [["ns1.mystub.example.net", ["192.0.2.4"]]]]]
stub2:
- - mystub2,example.net
- - - ns1.mystub2.example.net
- - 192.0.2.5
tasks:
- debug: var=stub
- debug: var=stub2

Gives:
TASK [debug] ***********************************
ok: [localhost] => {
"stub": [
[
"mystub.example.net",
[
[
"ns1.mystub.example.net",
[
"192.0.2.4"
]
]
]
]
]
}

TASK [debug] ***********************************
ok: [localhost] => {
"stub2": [
[
"mystub2,example.net",
[
[
"ns1.mystub2.example.net",
[
"192.0.2.5"
]
]
]
]
]
}

As you can see thees two are identical.

> Fully correct the yams represantion is:
>
>         stub2:
>           -
>             - mystub2,example.net
>             -
>               -
>                 - ns1.mystub2.example.net
>                 -
>                   - 192.0.2.
>         stub2:
>           - - mystub2,example.net
>             - - - - ns1.mystub2.example.net
>                 - - 192.0.2.5
>
> but that was easy to fix myself, so thanks again.

But this gives me

TASK [debug] **************************************
ok: [localhost] => {
"stub3": [
[
"mystub2,example.net",
[
[
[
"ns1.mystub2.example.net"
],
[
"192.0.2.5"
]
]
]
]
]
}

And this is *not* the same as your example in your first mail.


--
Kai Stian Olstad

Ralf Weber

unread,
Mar 5, 2019, 10:44:24 AM3/5/19
to ansible...@googlegroups.com
Moin!

On 4 Mar 2019, at 18:13, Kai Stian Olstad wrote:
> And this is *not* the same as your example in your first mail.
Your are of course correct. My eyesight must be deteriorating, sorry for
the useless email.
Reply all
Reply to author
Forward
0 new messages