# Use "with\_sublelements" with 3,4,5,... elements ?

**URL:** <https://forum.ansible.com/t/use-with-sublelements-with-3-4-5-elements/21402>\
**Category:** Ansible Developer\
**Created:** [May 11, 2016, 9:21am UTC](https://forum.ansible.com/t/use-with-sublelements-with-3-4-5-elements/21402 "2016-05-11T09:21:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clement\_VALENTIN](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Clement\_VALENTIN](https://forum.ansible.com/u/Clement_VALENTIN)\
**Post date:** [May 11, 2016, 9:21am UTC](https://forum.ansible.com/t/use-with-sublelements-with-3-4-5-elements/21402/1 "2016-05-11T09:21:35Z")

</div>

Hi,

I try to use function “with\_subelements” to create a folder tree for MySQL installation with multiple element in vars, but I can’t do this …

Exemple :

**_vars.yml :_**

> mysqld\_instance:
> 
> - name: “HOSTNAME”  
> port:
> - ‘3307’  
> user: ‘mysql’  
> mysql\_data\_path: ‘/opt/mysql’  
> mysql\_data\_tree:
> - { file: ‘run’, mode: ‘755’ }
> - { file: ‘var’, mode: ‘755’ }
> - { file: ‘var/log’, mode: ‘755’ }
> - { file: ‘var/data’, mode: ‘755’ }
> - { file: ‘var/tmp’, mode: ‘755’ }
> - { file: ‘var/iblogs’, mode: ‘755’ }
> - { file: ‘var/ibdata’, mode: ‘755’ }

**_task.yml :_**

> - name: “Create MySQL Tree”  
> file:  
> path: “{{ item.0.mysql\_data\_path }}/{{ item.1 }}/{{ item.2.file }}”  
> owner: “{{ item.0.user }}”  
> group: “{{ item.0.user }}”  
> mode: “{{ item.1.mode }}”  
> state: directory  
> with\_subelements:
> - “{{ mysqld\_instance }}”
> - port
> - mysql\_data\_tree

**_Output :_**

> fatal: [XXXXXXXXXXXXXXXXXXXX]: FAILED! =\> {“failed”: true, “msg”: “subelements lookup expects a list of two or three items, the optional third item must be a dict with flags skip\_missing”}

Someone would tell me how to do this?

Thx

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 11, 2016, 9:29am UTC](https://forum.ansible.com/t/use-with-sublelements-with-3-4-5-elements/21402/2 "2016-05-11T09:29:48Z")

</div>

​  
You are trying to point to two (or more) lists. The module just doesn't  
work like that. You need to target only one list.

---

<div class="post-metadata">

**Author:** ![Clement\_VALENTIN](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Clement\_VALENTIN](https://forum.ansible.com/u/Clement_VALENTIN)\
**Post date:** [May 11, 2016, 9:35am UTC](https://forum.ansible.com/t/use-with-sublelements-with-3-4-5-elements/21402/3 "2016-05-11T09:35:22Z")

</div>

Arf, and do you have an idea of how I Could make this with other function/module ?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 11, 2016, 9:46am UTC](https://forum.ansible.com/t/use-with-sublelements-with-3-4-5-elements/21402/4 "2016-05-11T09:46:57Z")

</div>

Unless you write your own lookup module, there is none.​

The ansible way right now would be to restructure your base data to make it  
work with the subelements module.

---

<div class="post-metadata">

**Author:** ![marc.petrivelli](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/marc.petrivelli/32/3773_2.png) [@marc.petrivelli](https://forum.ansible.com/u/marc.petrivelli)\
**Post date:** [May 11, 2016, 5:14pm UTC](https://forum.ansible.com/t/use-with-sublelements-with-3-4-5-elements/21402/5 "2016-05-11T17:14:56Z")

</div>

I hacked the subelement lookup plugin to accept an additional layer if list. If you’re interested take a look at [https://gist.github.com/mpetrive/6c8dcd77968209b7660f3c4b965808ea](https://gist.github.com/mpetrive/6c8dcd77968209b7660f3c4b965808ea)

---

<div class="post-metadata">

**Author:** ![Clement\_VALENTIN](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Clement\_VALENTIN](https://forum.ansible.com/u/Clement_VALENTIN)\
**Post date:** [May 12, 2016, 7:32am UTC](https://forum.ansible.com/t/use-with-sublelements-with-3-4-5-elements/21402/6 "2016-05-12T07:32:02Z")

</div>

Thx Marc,

Good job, but I did not really want to change ansible sources 😕  
Rather adapt my code and I hope Ansible integrates this kind of operation afterwards : p

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 12, 2016, 7:38am UTC](https://forum.ansible.com/t/use-with-sublelements-with-3-4-5-elements/21402/7 "2016-05-12T07:38:45Z")

</div>

​no need, just drop that plugin in lookup\_plugins/ next to your playbooks​
