# Selecting a subset from within with\_items

**URL:** <https://forum.ansible.com/t/selecting-a-subset-from-within-with-items/13275>\
**Category:** Ansible Project\
**Created:** [August 9, 2013, 8:10am UTC](https://forum.ansible.com/t/selecting-a-subset-from-within-with-items/13275 "2013-08-09T08:10:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kai\_Weber](https://avatars.discourse-cdn.com/v4/letter/k/f6c823/32.png) [@Kai\_Weber](https://forum.ansible.com/u/Kai_Weber)\
**Post date:** [August 9, 2013, 8:10am UTC](https://forum.ansible.com/t/selecting-a-subset-from-within-with-items/13275/1 "2013-08-09T08:10:36Z")

</div>

I have a task file to create users, add them to sudoers, enable ssh public key  
login. A minimal version looks like this

---

<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:** [August 9, 2013, 8:25am UTC](https://forum.ansible.com/t/selecting-a-subset-from-within-with-items/13275/2 "2013-08-09T08:25:20Z")

</div>

How I solve this is by definig users in several list:

---

<div class="post-metadata">

**Author:** ![Kai\_Weber](https://avatars.discourse-cdn.com/v4/letter/k/f6c823/32.png) [@Kai\_Weber](https://forum.ansible.com/u/Kai_Weber)\
**Post date:** [August 9, 2013, 12:13pm UTC](https://forum.ansible.com/t/selecting-a-subset-from-within-with-items/13275/3 "2013-08-09T12:13:29Z")

</div>

❦ Serge van Ginderachter \<serge@vanginderachter.be\>:

> Then depending on the task I do  
> with\_flattened:  
> - users\_dev  
> - users\_admin  
> - ..

Where can I read about with\_flattened?

But maybe this will not work for me. If I have users who are admins and devs I have to  
duplicate their definition?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 9, 2013, 12:23pm UTC](https://forum.ansible.com/t/selecting-a-subset-from-within-with-items/13275/4 "2013-08-09T12:23:34Z")

</div>

I’m not sure why Serge is showing with\_flattened here, when you wanted to iterate across ssh keys per user, rather than walk across two lists.

I’ve got a ticket open to create a lookup plugin more appropriate to making this easier, when there is a data structure of users.

[https://github.com/ansible/ansible/issues/3752](https://github.com/ansible/ansible/issues/3752)

We also have a ticket to make sure all the with\_\* features have examples in the 1.3 release.

---

<div class="post-metadata">

**Author:** ![Kai\_Weber](https://avatars.discourse-cdn.com/v4/letter/k/f6c823/32.png) [@Kai\_Weber](https://forum.ansible.com/u/Kai_Weber)\
**Post date:** [August 9, 2013, 12:48pm UTC](https://forum.ansible.com/t/selecting-a-subset-from-within-with-items/13275/5 "2013-08-09T12:48:28Z")

</div>

❦ Michael DeHaan \<michael@ansibleworks.com\>:

> I've got a ticket open to create a lookup plugin more appropriate to making  
> this easier, when there is a data structure of users.

with\_subelements sounds like a nice addition.

But what I want is having a list of all users in a vars file and then for  
a certain host to only create this and that user.

users:  
&nbsp;&nbsp;&nbsp;foo:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "Foo Bar"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssh\_key: "ssh-rsa ..."  
&nbsp;&nbsp;&nbsp;bar:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "Bar Foo"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssh\_key: "ssh-rsa ..."  
&nbsp;&nbsp;&nbsp;foobar:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "Barbara Foo"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssh\_key: "ssh-rsa ..."

On host alpha

- authorized\_key: name={{ item.name }} key={{ item.ssh\_key }}  
&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- users.foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- users.bar

On host beta

- authorized\_key: name={{ item.name }} key={{ item.ssh\_key }}  
&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- users.foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- users.foobar

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [August 9, 2013, 1:10pm UTC](https://forum.ansible.com/t/selecting-a-subset-from-within-with-items/13275/6 "2013-08-09T13:10:22Z")

</div>

Would this help

- hosts: localhost  
vars:  
users:
- name: benno  
last: joy  
host: abc
- name: jj  
last: jb  
host: bcd
- name: benz  
last: joy  
host: abc

tasks:

- user: name=[item.name](http://item.name)  
with\_items: users  
when: item.host == inventory\_hostname

Instead of host maybe use a group
