3

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

this is not working. with_list: "{{ lookup('subelements', org1, 'id', skip_missing=True) }} + {{ lookup('subelements', org2, 'id', skip_missing=True) }}". While doing so, I get only "(" as output.
My bad. I had 2 extra { on the second line. I have updated my answer to remove them.
While "{" removed, it looks now with_list: "{{ lookup('subelements', org1, 'id', skip_missing=True) + lookup('subelements', org2, 'id', skip_missing=True) }}" that has error throwing by ansible. {"failed": true, "msg": "Unexpected templating type error occurred: can only concatenate list (not \"tuple\") to list"}
Hmm. I'm using Ansible 2.5.1. I wonder if that was an update made in a version of Ansible that is newer than yours. If so, you can debug what {{ item }} contains and then update to access the correct element in what your version of Ansible gives you. If you can upgrade your version of Ansible, even better.
You were correct @Lewis. Found the issue now. While executing, for Org1 there are only single entry in the example, that caused creating junc. While added another line, it worked well. I dont know the logic behind this, but thats how my issue got fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.