How can I concatenate a contents of several files into a variable?
Here's the problem: I'm trying to set public keys for a user on a remote machine. The example from the authorized_key documentation that almost works:
- name: Set up authorized_keys for the deploy user
authorized_key: user=deploy
key="{{ item }}"
with_file:
- public_keys/doe-jane
- public_keys/doe-john
But in fact I need to use exclusive=yes
, so after the update all non-provided public keys are removed.
If the exclusive=yes
is provided then only the last public key listed remains in the .ssh/authorized_keys
file (also reported as a bug).
My current approach:
- name: create empty temporary keys file
local_action: "shell > /tmp/auth_keys"
- name: concat keys to temporary file
local_action: "shell echo {{ item }} >> /tmp/auth_keys"
with_file:
- public_keys/doe-jane
- public_keys/doe-john
- name: set up authorized_keys
authorized_key: user=deploy
key="{{ lookup('file', '/tmp/auth_keys') }}"
exclusive=yes
This works but the first two commands always produce "changed". Also I feel there must be a more elegant solution for this.
So, is there a way how to concatenate contents of several files into a variable? Or is there any better approach in general for this task?