Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible loop values using with_list with multiple variables

Tags:

ansible

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

like image 658
Rio Avatar asked Jan 01 '26 14:01

Rio


1 Answers

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.

like image 129
Lewis M Avatar answered Jan 06 '26 11:01

Lewis M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!