# Defining complex data (cascaded arrays) in yaml has problems

**URL:** <https://forum.ansible.com/t/defining-complex-data-cascaded-arrays-in-yaml-has-problems/29817>\
**Category:** Ansible Project\
**Created:** [March 4, 2019, 11:56am UTC](https://forum.ansible.com/t/defining-complex-data-cascaded-arrays-in-yaml-has-problems/29817 "2019-03-04T11:56:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ralf\_Weber](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@Ralf\_Weber](https://forum.ansible.com/u/Ralf_Weber)\
**Post date:** [March 4, 2019, 11:56am UTC](https://forum.ansible.com/t/defining-complex-data-cascaded-arrays-in-yaml-has-problems/29817/1 "2019-03-04T11:56:17Z")

</div>

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  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Set some test facts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bla: True  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stub: [["mystub.example.net", [["ns1.mystub.example.net", ["192.0.2.4"]]]]]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stub2:

---

<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:** [March 4, 2019, 3:55pm UTC](https://forum.ansible.com/t/defining-complex-data-cascaded-arrays-in-yaml-has-problems/29817/2 "2019-03-04T15:55:16Z")

</div>

> 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

&nbsp;&nbsp;stub2:  
&nbsp;&nbsp;&nbsp;&nbsp;- - mystub2,example.net  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- - - ns1.mystub2.example.net  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- - 192.0.2.5

or

&nbsp;&nbsp;stub2:

---

<div class="post-metadata">

**Author:** ![Ralf\_Weber](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@Ralf\_Weber](https://forum.ansible.com/u/Ralf_Weber)\
**Post date:** [March 4, 2019, 4:44pm UTC](https://forum.ansible.com/t/defining-complex-data-cascaded-arrays-in-yaml-has-problems/29817/3 "2019-03-04T16:44:41Z")

</div>

Moin!

---

<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:** [March 4, 2019, 5:13pm UTC](https://forum.ansible.com/t/defining-complex-data-cascaded-arrays-in-yaml-has-problems/29817/4 "2019-03-04T17:13:56Z")

</div>

> Moin!
> 
> > 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  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;stub: [["mystub.example.net", [["ns1.mystub.example.net", ["192.0.2.4"]]]]]  
&nbsp;&nbsp;&nbsp;&nbsp;stub2:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- - mystub2,example.net  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- - - ns1.mystub2.example.net  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- - 192.0.2.5  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=stub  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=stub2

Gives:  
TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"stub": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mystub.example.net",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ns1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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ns1.mystub2.example.net",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"192.0.2.5"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}

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] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"stub3": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mystub2,example.net",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ns1.mystub2.example.net"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"192.0.2.5"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}

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

---

<div class="post-metadata">

**Author:** ![Ralf\_Weber](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@Ralf\_Weber](https://forum.ansible.com/u/Ralf_Weber)\
**Post date:** [March 5, 2019, 10:29am UTC](https://forum.ansible.com/t/defining-complex-data-cascaded-arrays-in-yaml-has-problems/29817/5 "2019-03-05T10:29:20Z")

</div>

Moin!
