# Using with\_dict with a loop and missing key values...

**URL:** <https://forum.ansible.com/t/using-with-dict-with-a-loop-and-missing-key-values/23605>\
**Category:** Ansible Project\
**Created:** [January 31, 2017, 1:46pm UTC](https://forum.ansible.com/t/using-with-dict-with-a-loop-and-missing-key-values/23605 "2017-01-31T13:46:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raphael\_Rabelo\_de\_Ol](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@Raphael\_Rabelo\_de\_Ol](https://forum.ansible.com/u/Raphael_Rabelo_de_Ol)\
**Post date:** [January 31, 2017, 1:46pm UTC](https://forum.ansible.com/t/using-with-dict-with-a-loop-and-missing-key-values/23605/1 "2017-01-31T13:46:11Z")

</div>

Hi guys,

I’m trying to use a loop and with\_dict together, but I have an issue with the value of some of key are missing, for example the value ‘ssh\_key’:  
It’s is proposital because i don’t wanna any key in file.

The ‘when’ conditional don’t can evaluate if my variable exists ‘item.value.ssh\_key.exists’:

**My dict:**

users:  
user1:  
home: “/home/user1”  
uid: 5000  
gid: 5000  
ssh\_key: “{{ role\_path }}/files/keys/user1.pub”

key\_options : ‘from=“192.168.1.2”,no-agent-forwarding,no-port-forwarding,no=X11-forwarding’

user2:  
home: “/var/www/user2”  
uid: 8000  
gid: 8000  
key\_options : ‘from=“192.168.1.2”,no-agent-forwarding,no-port-forwarding,no=X11-forwarding’

**My task:**

- name: Configuring authorized\_keys  
authorized\_key:  
user : “{{ item.key }}”  
key : “{{ lookup(‘file’, ‘{{ item.value.ssh\_key }}’) }}”  
key\_options : “{{ item.value.key\_options }}”  
with\_dict: “{{ users }}”  
when:
- item.value.ssh\_keys.exists == true

Is there some way to make it work properly ?

Thanks!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 31, 2017, 8:19pm UTC](https://forum.ansible.com/t/using-with-dict-with-a-loop-and-missing-key-values/23605/2 "2017-01-31T20:19:39Z")

</div>

Probably, but you have provided to little information.  
What is your error? You have not provided any information about what went wrong.  
Without it must people must create a replica of your task to find out what happened, and in most cases people doesn't have the time to do it.

So please, always show us the complete error message.

---

<div class="post-metadata">

**Author:** ![Jinesh\_Choksi](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jinesh\_Choksi](https://forum.ansible.com/u/Jinesh_Choksi)\
**Post date:** [February 1, 2017, 9:37am UTC](https://forum.ansible.com/t/using-with-dict-with-a-loop-and-missing-key-values/23605/3 "2017-02-01T09:37:38Z")

</div>

Your dict has a key called “ssh\_key” but the “when” clause in the task says: “- item.value.ssh\_key **s**.exists == true”

Notice the extra ‘s’ in ssh\_keys. Is that correct?
