# working with multiple lists.. one defining the other?

**URL:** <https://forum.ansible.com/t/working-with-multiple-lists-one-defining-the-other/17587>\
**Category:** Ansible Project\
**Created:** [February 21, 2015, 2:05am UTC](https://forum.ansible.com/t/working-with-multiple-lists-one-defining-the-other/17587 "2015-02-21T02:05:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_Parker](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@Joel\_Parker](https://forum.ansible.com/u/Joel_Parker)\
**Post date:** [February 21, 2015, 2:05am UTC](https://forum.ansible.com/t/working-with-multiple-lists-one-defining-the-other/17587/1 "2015-02-21T02:05:59Z")

</div>

I already have tasks for creating users, groups, authorized\_keys, etc. from the existing lists of users, by setting a variable that’s linked to each of those.

What I want to do is have group\_vars for an inventory that lists which groups I want added to servers. And then another inventory might have other group\_vars where the groups are different. I want to define all users and what groups they’re in in users.yml, and then simply specify which groups get different access to servers in my inventory by creating my user\_groups list in a group\_var.

in group\_vars/users.yml:

devs:

- “{{ ronda }}”
- “{{ abe }}”
- “{{ kathy }}”

sysadmins:

- “{{ york }}”
- “{{ kathy }}”
- “{{ george }}”

managers:

- “{{ brown }}”
- “{{ lee }}”

ronda:

- { name: “ronda”, fullname: “Ronda”, uid: “1001”, gid: “1001”, state: “present”, remove: “no”, ssh\_key: “…/ssh\_keys/ronda.pub” }  
etc…

in inventory/group\_vars/single-environment.yml:

user\_groups:

- devs
- sysadmins
- managers

There could be another file in inventory/group\_vars/second-environment.yml that defines user\_groups differently:

user\_groups:

- sysadmins

Is this possible, at all? I feel like I should be able to reference this doing something like user\_groups[item.name], user\_groups[item.fullname], user\_groups[uid], etc. I know that method isn’t possible, but, there’s gotta’ be a way?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [February 21, 2015, 2:10pm UTC](https://forum.ansible.com/t/working-with-multiple-lists-one-defining-the-other/17587/2 "2015-02-21T14:10:49Z")

</div>

I’m actually using this very way of defining users and groups myself.

What you want to do is possible if you flatten the lists in user\_groups using the flattened lookup.

Until now, you could only do that in a with\_flattened lookup construct, but thanks to Brian’s recent patch, you can now also do it (in latet devel) in a templating lookup():

[https://github.com/ansible/ansible/pull/10298](https://github.com/ansible/ansible/pull/10298)

See also [https://github.com/ansible/ansible/issues/10291](https://github.com/ansible/ansible/issues/10291) and [https://github.com/ansible/ansible/issues/10291](https://github.com/ansible/ansible/issues/10291)

Serge

---

<div class="post-metadata">

**Author:** ![Joel\_Parker](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@Joel\_Parker](https://forum.ansible.com/u/Joel_Parker)\
**Post date:** [February 22, 2015, 7:49pm UTC](https://forum.ansible.com/t/working-with-multiple-lists-one-defining-the-other/17587/3 "2015-02-22T19:49:35Z")

</div>

Thank you for the response. Can you provide a quick example of how you use the flattening lookup to accomplish this.. particularly showing the structure of the data you're using?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [February 23, 2015, 8:33am UTC](https://forum.ansible.com/t/working-with-multiple-lists-one-defining-the-other/17587/4 "2015-02-23T08:33:56Z")

</div>

Given your example, pretty simple:

with\_flattened: user\_groups

But that only works to feed a task. The new feature I referred to, is brand new, I didn’t use it yet, but that would roughly translate to

{{ lookup(‘flattened’, user\_groups, want\_list=True) }}
