Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
See… – Mxx Jun 6 '14 at 4:41
up vote 2 down vote accepted

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/') }}" state=present

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.