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

1,261 views
Skip to first unread message

Clément VALENTIN

unread,
May 11, 2016, 5:21:35 AM5/11/16
to Ansible Development
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

Serge van Ginderachter

unread,
May 11, 2016, 5:30:11 AM5/11/16
to Clément VALENTIN, Ansible Development
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.


Clément VALENTIN

unread,
May 11, 2016, 5:35:22 AM5/11/16
to Ansible Development, cle...@valent1.fr
Arf, and do you have an idea of how I Could make this with other function/module ?

Serge van Ginderachter

unread,
May 11, 2016, 5:47:18 AM5/11/16
to Clément VALENTIN, Ansible Development

On 11 May 2016 at 11:35, Clément VALENTIN <cle...@valent1.fr> wrote:
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.


Marc Petrivelli

unread,
May 11, 2016, 1:14:56 PM5/11/16
to Ansible Development
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 

Clément VALENTIN

unread,
May 12, 2016, 3:32:02 AM5/12/16
to Ansible Development
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

Serge van Ginderachter

unread,
May 12, 2016, 3:39:07 AM5/12/16
to Clément VALENTIN, Ansible Development

On 12 May 2016 at 09:32, Clément VALENTIN <cle...@valent1.fr> wrote:
Good job, but I did not really want to change ansible sources :/

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

Reply all
Reply to author
Forward
0 new messages