Dennis
Dennis

Reputation: 11

Ansible ad hoc lookup

ansible all -m authorized_key -a 'name=automation key="{{lookup('file','/home/automation/.ssh/id.pub') }}' -C

ansible all -m authorized_key -a 'name=automation key="{{lookup('file','/home/automation/.ssh/id.pub') }}' -CD

ansible all -m authorized_key -a 'name=automation key=' {{lookup('file','/home/automation/.ssh/id.pub') }}' -CD

ansible all -m authorized_key -a "name=automation key=' {{lookup('file','/home/automation/.ssh/id.pub') }}" -CD

ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: name=automation key=' {{lookup('file','/home/automation/.ssh/id.pub') }}

I tried all of those combination but none is working.
What is the correct way to achieve this?

Upvotes: 1

Views: 1338

Answers (2)

Zeitounator
Zeitounator

Reputation: 44808

You must escape quotes in your shell AND make sure everything is OK on ansible side once received. In this case, using single quotes as the outermost quoting is probably the hardest choice. Here are five (non exhaustive) possible solutions (using double quotes as outermost quoting). The first proposition is obviously the easiest.

Note as well that the authorized_key module does not have any name parameter hence I fixed the below examples using the user required parameter instead.

ansible all -m authorized_key -a "user=automation key={{ lookup('file\', '/home/automation/.ssh/id.pub\') }}" -CD

ansible all -m authorized_key -a "user=automation key='{{ lookup(\"file\", \"/home/automation/.ssh/id.pub\") }}'" -CD

ansible all -m authorized_key -a "user=automation key='{{ lookup(\\'file\\', \\'/home/automation/.ssh/id.pub\\') }}'" -CD

ansible all -m authorized_key -a "user=automation key=\"{{ lookup('file', '/home/automation/.ssh/id.pub') }}\"" -CD

ansible all -m authorized_key -a "user=automation key=\"{{ lookup(\\\"file\\\", \\\"/home/automation/.ssh/id.pub\\\") }}\"" -CD

Upvotes: 4

Vsevolod
Vsevolod

Reputation: 687

As module arguments (-a) imply that everything after <argument_name>= is a string, you don't need aditional quotes after '=' and can use lookup without any extra quotes around it:

ansible all -m authorized_key -a "name=automation key={{ lookup('file', '/home/automation/.ssh/id.pub') }}"

or

ansible all -m authorized_key -a 'name=automation key={{ lookup("file", "/home/automation/.ssh/id.pub") }}'

Just make sure that you use different type of quotes.

Think of module arguments in -a as same arguments in yaml format you don't need to protect them unless their value starts with special character.

Upvotes: 1

Related Questions