# use file from host as input for lineinfile module

I'm currently setting up a system with ansible and want to automate it as much as possible.

So I'm creating the public keys on my ansible machine and copy them to the hosts so that I can control the hosts as much as possible from the outside.

Now I need to setup connectivity between my hosts via public keys. I'm using the lineinfile module to ensure that the keys are present in the authorized_keys and known_hostsfiles. Is there a way to read the contents of a file (on the machine from where ansible is run) and use it as input for the lineinfile module?

-
–  Mxx Jun 6 '14 at 4:41

## 1 Answer

As Mxx pointed out in the comments, lookup is the answer. Furthermore, I would not recommend using the lineinfile for adding authorized_keys. There is already an Ansible module specifically for that, and it's a lot less likely to get you into trouble. Here's an example play. It will grab from the local public key, and properly make sure it exists for the target user.

tasks: - name: Install SSH authorized key authorized_key: > user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}" state=present

-