Rio
Rio

Reputation: 672

Ansible loop values using with_list with multiple variables

I am trying to create list of users using different variables in same code as list of variables.

The variables i have defined as below:

org1:
  - { name: 'Sales',         id: "{{ 'john,mike'.split(',') }}"}

org2:
  - { name: 'Testing',       id: "{{ 'samy,jazz'.split(',') }}"}
  - { name: 'dept303',       id: "{{ 'doug'.split(',') }}"}

The code i have written as below. This code is working while i am using single variable.

- name: Create users entry
  lineinfile:
    dest: "/etc/vsftpd_users/user_list"
    line: "{{ item.1 | lower }}"
  with_list: "{{ lookup('subelements', org1, 'id', skip_missing=True) }}"

but not working while trying to use as list. It creates a long junk line with all converted values.

with_list:
- "{{ lookup('subelements', org1, 'id', skip_missing=True) }}"
- "{{ lookup('subelements', org2, 'id', skip_missing=True) }}"

Is there any other way to do so? or i am missing something here. Please help me me out here. Thank you.

My Ansible version: ansible 2.2.1.0

Upvotes: 3

Views: 2334

Answers (1)

Lewis M
Lewis M

Reputation: 548

You are making lists of lists. If you just did

with_lists:
- "{{ lookup('subelements', org1, 'id', skip_missing=True) }}"

you would still have the same issue.

What you want to do is concatenate them together

with_lists: "{{ lookup('subelements', org1, 'id', skip_missing=True) + 
                lookup('subelements', org2, 'id', skip_missing=True) }}"

Hope this helps.

Upvotes: 2

Related Questions