Use "with_sublelements" with 3,4,5,... elements ?

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


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.

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

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.

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

Thx Marc,

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

​no need, just drop that plugin in lookup_plugins/ next to your playbooks​