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
Written by Ramon de la Fuente
Related protips
1 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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Lookup
Authors
Saager Mhatre
2.576K
sjeanpierre
1.11K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#