I have a fairly simple playbook which creates authorized_keys
entries for a given user in Ansible:
- name: chat user authorized keys
authorized_key:
user: chat
key: |
{% for filename in lookup('fileglob', 'public_keys/*.pub') %}
# {{ filename }}
{{ lookup('file', filename ) }}
{% endfor %}
exclusive: true
I have around six public key files in that directory. I'm trying to format a single file content with all of the keys delimited by newlines.
This is what is suggested by the Ansible docs:
exclusive
Whether to remove all other non-specified keys from the authorized_keys file. Multiple keys can be specified in a single key string value by separating them by newlines. This option is not loop aware, so if you use
with_
, it will be exclusive per iteration of the loop, if you want multiple keys in the file you need to pass them all tokey
in a single batch as mentioned above.
How can I use a fileglob to concatenate all of the files matching public_keys/*.pub
into a single key here so that I can maintain exclusivity and properly remove keys when necessary?