Using with_dict with a loop and missing key values...

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!

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.

Your dict has a key called “ssh_key” but the “when” clause in the task says: “- item.value.ssh_keys.exists == true”

Notice the extra ‘s’ in ssh_keys. Is that correct?