Ansible can not locate file in lookup when using a tilde in path

Viewed 617

Using the default directory in Ansible, I set the variable:

vm_public_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

I have a task that use a Jinja template, This template will use the vm_public_key variable.
But I have an error:

"msg": "AnsibleError: An unhandled exception occurred while templating '{{ lookup('file', '~/.ssh/id_rsa.pub') }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: ~/.ssh/id_rsa.pub"

To summarize, It can't find the ssh key.
The workaround was to replace the tilde ~ with the home directory /home/MY_USER. But I need to replace it with something more flexible, like the tilde.

The command I use to run the ansible :

sudo ansible-playbook -K playbook.yml -i hosts --connection=local

1 Answers

You can use {{ lookup('env', 'HOME') }} to retrieve the value of $HOME which is equal to the tilde.

Related