Last Updated: February 25, 2016
·
2.962K
· ramondelafuente

Ansible lineinfile with lookup()

There are a few situations where Ansible's lineinfile module is useful, but complicated because you don't know the exact line that should exist.

You might end up using a template, or a command that registers a variable, but if the line you wish to add is the result of a command on the host you can use the lookup plugin and save yourself a an extra task.

One example is adding a host to the known_hosts file:

lineinfile: dest=/etc/ssh/ssh_known_hosts 
    line="{{lookup('pipe', 'ssh-keyscan github.com') }}" 
    state=present 
    create=yes mode=0644

1 Response
Add your response

Heads-up: this is broken in Ansbile 1.5.1 because of the way shell commands are started. Fixed in 1.5.3 and up.

over 1 year ago ·