# Using with\_dict but skipping certain items

**URL:** <https://forum.ansible.com/t/using-with-dict-but-skipping-certain-items/19950>\
**Category:** Ansible Project\
**Created:** [December 7, 2015, 4:13pm UTC](https://forum.ansible.com/t/using-with-dict-but-skipping-certain-items/19950 "2015-12-07T16:13:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Stewart](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@David\_Stewart](https://forum.ansible.com/u/David_Stewart)\
**Post date:** [December 7, 2015, 4:13pm UTC](https://forum.ansible.com/t/using-with-dict-but-skipping-certain-items/19950/1 "2015-12-07T16:13:09Z")

</div>

Hi,

I am new to Ansible and am having difficulty in understanding how a loop created with a ‘with\_dict’ directive can be filtered based upon values within the dictionary.

For instance, I want a dictionary of users that need to be put into a specific state, e.g.:

users:

testuser1: {state: absent, public\_key\_file: “…/keys/dev-env.pub”}

testuser2: {state: present, public\_key\_file: “…/keys/dev-env.pub”}

I then have a task that loops over this map:

- name: Manage Users  
user:

name={{ item.key }}  
state={{ item.value.state }}  
remove=yes  
with\_dict: users | default({})

This will add or remove users depending on the ‘users’ dictionary (e.g. testuser1 will be removed and testuser2 created)

I now want to use the same data definition to loop over users and put any authorized keys in place:

- name: Deploy User Keys  
sudo: yes

authorized\_key:  
user={{ item.key }}  
key=“{{ lookup(‘file’, item.value.public\_key\_file) }}”  
with\_dict: users | default({})

this would work apart from the fact that the ‘testuser1’ home dir no longer exists.

What I would like to do is to skip users with a ‘state’ of ‘absent’. Is there a way to do this or am I going about this in completely the wrong way?

Thanks,  
Dave.
