community.libvirt
community.libvirt copied to clipboard
fix resolve tilde in file paths (fixes #161)
SUMMARY
The libvirt_qemu connection module uses QEMU guest agent to copy files and run commands on the remote host. Ansible uses the '~/' symbolic path to the user's home directory for many commands and file paths. Unfortunately, since QEMU guest agent doesn't run in a shell, it doesn't resolve the '~/' path to the user's homedir.
Instead, the Ansible paths end up at '/~/' in the remote host. This breaks some assumptions for some modules about the path to the Ansible files. Some modules use the resolved homedir path and fail to find any files in that location.
This fix first resolves the remote user homedir with a QEMU guest command, caches the result, and then replaces '~/' found in any command or remote file paths with the resolved absolute path to the user's homedir.
ISSUE TYPE
- Bugfix Pull Request
COMPONENT NAME
community.libvirt.libvirt_qemu connection plugin
ADDITIONAL INFORMATION
I've tested this patch with playbooks that use many different Ansible modules and it seems to work in all the ones I tested, including the copy module that was having issues in #161.
This looks good to me, it fixes the issue #161. Can we get this merged?
Hi @antonc42 thanks for the contribution! I've been trying to replicate this and finally did on Ubuntu, see my comment in the issue here https://github.com/ansible-collections/community.libvirt/issues/161#issuecomment-1913109726
I think it may be a problem with dash
being the default shell, it seems to work as expected if I switch the default shell to bash
. Would appreciate if you can see if that also works for you.
In terms of a patch, I would probably prefer to see if there's another way to solve it rather than using specific Linux commands and regex, if possible. Not sure if something like os.path.expanduser
can help on the remote side. However if we do have to use commands, then we may need some if checks for the OS as I'm not sure that the patch as it is would work on Windows guests.
/cc @leegarrett
@csmart
I'm not sure if it's possible to use os.path.expanduser
since that would be executed on the Ansible controller instead of in the guest VM, correct? The user homedir inside the VM is needed to resolve the tilde. The only way I knew of to interact with the guest VM is through commands. Is there another way?
I suppose I could execute a Python command on the guest VM, e.g. python3 -c 'import os; print(os.path.expanduser("~"))'
. I'm not sure if that is universal enough to run on Windows guests as I don't have any experience with Python on Windows.
Not needed. See my comment in the original issue #161.