Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with file paths when using ansible.builtin.copy module #161

Open
antonc42 opened this issue Sep 22, 2023 · 1 comment
Open

Problem with file paths when using ansible.builtin.copy module #161

antonc42 opened this issue Sep 22, 2023 · 1 comment

Comments

@antonc42
Copy link

SUMMARY

Almost all Ansible operations appear to use the unresolved path to the Ansible temp directory '~/.ansible/tmp'. However, the ansible.builtin.copy module appears to resolve this path to '/root/.ansible/tmp' when looking for the source file. The underlying problem is that when I examined the filesystem on the VM, it appears that the community.libvirt.libvirt_qemu connection plugin is copying all files in the temp directory to '/~/.ansible/tmp'. The tilde is not being resolved to the homedir of the user and instead ends up in a '/~' directory at the root of the filesystem.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

community.libvirt.libvirt_qemu connection plugin

ANSIBLE VERSION
ansible [core 2.15.4]
  config file = /home/user/ansible-dir/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/ansible-dir/.venv/lib/python3.11/site-packages/ansible
  ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/user/ansible-dir/.venv/bin/ansible
  python version = 3.11.5 (main, Aug 25 2023, 13:19:50) [GCC 11.4.0] (/home/user/ansible-dir/.venv/bin/python)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
# /home/user/ansible-dir/.venv/lib/python3.11/site-packages/ansible_collections
Collection        Version
----------------- -------
community.libvirt 1.2.0  
CONFIGURATION
CONFIG_FILE() = /home/user/ansible-dir/ansible.cfg
DEFAULT_HOST_LIST(/home/user/ansible-dir/ansible.cfg) = ['/home/user/ansible-dir/inventory.yml']
DEFAULT_KEEP_REMOTE_FILES(env: ANSIBLE_KEEP_REMOTE_FILES) = False
EDITOR(env: EDITOR) = /home/user/bin/vim
OS / ENVIRONMENT

Target OS: Ubuntu 22.04 Server

STEPS TO REPRODUCE
ansible -i local-libvirt-inventory.yml --become all -m copy -a 'src=srcfile dest=/path/to/dest/file'
EXPECTED RESULTS

The local file is copied to the libvirt host.

ACTUAL RESULTS

The file is not copied. When examining the filesystem in the libvirt VM, I found that the directory '/root/.ansible/tmp' doesn't exist. It is located at '/~/.ansible/tmp' instead.

libvirt-host | FAILED! => {
    "changed": false,
    "checksum": "fac0b7e52fc298c5c5feb6b7673f74e2306aba6e",
    "msg": "Source /root/.ansible/tmp/ansible-tmp-1695394376.1436307-368511-193439908275987/source not found"
}
antonc42 added a commit to antonc42/community.libvirt that referenced this issue Sep 22, 2023
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 assumtions 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.
@csmart
Copy link
Collaborator

csmart commented Jan 27, 2024

@antonc42 hi, thanks for posting this issue. I've been trying to replicate it and didn't have luck with various versions of ansible and python on Fedora and CentOS. I spun up an Ubuntu 22.05 host and was able to replicate the issue.

After some digging, I think this is related to Ubuntu's use of dash as the default for /bin/sh on the guest. Are you able to try with bash and see if you have any different result?

Should be able to just set Use dash as the default system shell to no and retry without your patch.

sudo dpkg-reconfigure dash

Let me know how you go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants