Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ansible

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

like image 715
Hamza Ince Avatar asked Oct 24 '25 02:10

Hamza Ince


1 Answers

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

like image 110
Tom Avatar answered Oct 26 '25 01:10

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!