Ansible - how to concatenate files contents into a variable
There's nothing overly wrong with your first option and then, as your comment mentions, simply using changed_when: False
to acknowledge that this isn't something that you care about the result of it changing is a valid option.
To answer the actual question title you can, as mentioned in the GitHub "issue" you linked, simply concatenate the lookups directly in the task like so:
- name: set up authorized_keys
authorized_key: user=deploy
key="{{ lookup('file', 'public_keys/doe-jane') + lookup('file', 'public_keys/doe-john')}}"
exclusive=yes
However, a cleaner option may be to use the assemble module to concatenate your keys.
This would then change your current approach into something more like:
- name: create concatenated keys file
local_action: "assemble src=roles/ssh_keys/files/ssh_keys/ dest=/tmp/ssh_keys_file"
- name: set up authorized_keys
authorized_key: user=deploy
key="{{ lookup('file', '/tmp/ssh_keys_file' }}"
exclusive=yes
This will only be marked as changed if the destination file has changed at all so running it over and over leaves a lovely wall of green.
This relies on your ssh keys all being files and in the same folder (assemble is typically used for turning conf.d style directories into a single .conf file for programs that don't use the conf.d style configuration) but this is probably the most sensible way of holding them anyway.
The benefit of this is that you can simply add/remove ssh keys from the folder specified and it will be picked up on the next play without any need to add/remove keys being explicitly defined in the task itself as well.