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

44 views
Skip to first unread message

Raphael Rabelo de Oliveira

unread,
Jan 31, 2017, 8:50:27 AM1/31/17
to Ansible Project
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!

Kai Stian Olstad

unread,
Jan 31, 2017, 3:19:55 PM1/31/17
to ansible...@googlegroups.com
On 31. jan. 2017 14:46, Raphael Rabelo de Oliveira wrote:
> 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 ?

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.

--
Kai Stian Olstad

Jinesh Choksi

unread,
Feb 1, 2017, 4:37:38 AM2/1/17
to Ansible Project
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?
Reply all
Reply to author
Forward
0 new messages