authorized_key : How to put default value or bypass it

Hi

I’m using the authorized_key module in my ansible user role.
As some user aren’t ssh key yet, I would like that by default this key is not created and the task escaped.
I’ve seen that the key string is required by this module.
Is means that I cannot put a false default value?

Here’s my main task:
`
[…]

  • name: “add public keys to users”
    authorized_key:
    user: ‘{{ item.name }}’
    key: ‘{{ item.ssh_key }}’
    ignore_errors: yes
    with_items: ‘{{ user }}’
    […]
    `

Here’s the definition yaml file:
`
[…]
user:

  • name: ‘bob’
    password: “{{ bob_pass }}”
    comment: ‘Bob McKenzie’
    uid: ‘2000’
    group: ‘bob’
    groups: ‘wheel’
    shell: ‘/bin/bash’
    state: “{{ ‘present’ if bob_pass else ‘absent’ }}”
    ssh_key: “{{ bob_key | default(false) }}”
    […]
    `

The bob_key is define in an another yaml variable file.

Here’s the error:
`
[WARNING]: The value False (type bool) in a string field was converted to ‘False’ (type string). If this does not look like what you expect, quote the entire value to ensure it does not change.

failed: [localhost] (item={‘name’: ‘tzdkom’, ‘password’: …
… “msg”: “invalid key specified: False”}
`

Is the variable ssh_key: “{{ bob_key | default(false) }}” wrong ?
Could you please explain me this?
Has someone an idea to bypass this problem when no ssh_key exist?

Thanks and best regards,
M.

Just skip the item when ssh_key is not available (`false` in your example):

- name: "add public keys to users"
  authorized_key:
    user: '{{ item.name }}'
    key: '{{ item.ssh_key }}'
  with_items: '{{ user }}'
  when: item.ssh_key

That’s exactly what I need. I really hadn’t seen that.

Thanks a lot