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

win_copy fails remote copy of empty source directory #522

Open
vdcloudcraft opened this issue Jun 27, 2023 · 2 comments
Open

win_copy fails remote copy of empty source directory #522

vdcloudcraft opened this issue Jun 27, 2023 · 2 comments

Comments

@vdcloudcraft
Copy link

SUMMARY

win_copy fails remote copy of empty source folder to a non-existent dest folder

ISSUE TYPE
  • Bug Report
COMPONENT NAME

win_copy

ANSIBLE VERSION
ansible [core 2.14.4]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /home/runner/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.16 (main, Dec 21 2022, 10:57:18) [GCC 8.5.0 20210514 (Red Hat 8.5.0-17)] (/usr/bin/python3.9)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
# /usr/share/ansible/collections/ansible_collections
Collection      Version
--------------- -------
ansible.windows 1.12.0
CONFIGURATION
CONFIG_FILE() = /etc/ansible/ansible.cfg
OS / ENVIRONMENT

Tested on Windows Server 2019

STEPS TO REPRODUCE

Create an empty folder and try to copy it to a non-existent folder on the same remote machine, having both paths end with slashes.

According to the documentation, this should create an empty folder at dest

- name: Fail empty copy
  hosts: someWindowsHost
  tasks:
    - name: Copy empty dir
      ansible.windows.win_copy:
        remote_src: true
        src: C:\emptyDir\
        dest: C:\emptyDir2\
EXPECTED RESULTS

An empty folder is created.

ACTUAL RESULTS

Ansible/PowerShell fails with the message below. The reason seems to be fairly straightforward as well.

Here the module runs Get-ChildItem against the source, which is an empty folder and thus will return nothing. In turn the loop in the next line will not run and never call the Copy-Folder function, which would create the new folder at dest.

A few lines further down $result.size = Get-FileSize -Path $remote_dest will fail, since there is nothing to check.

An exception occurred during task execution. To see the full traceback, use -vvv. The error was:    at Microsoft.PowerShell.Commands.GetItemCommand.ProcessRecord()
fatal: [<redacted>]: FAILED! => {"changed": false, "dest": "C:\\emptyDir2\\", "msg": "Unhandled exception while executing module: Cannot find path 'C:\\emptyDir2\\' because it does not exist.", "src": "C:\\emptyDir\\"}
@jborean93
Copy link
Collaborator

Looking at the behaviour of copy I can see the following

  • If emptyDir2 doesn't exit then it will be created (it itself will be empty)
  • If emptyDir2 does exist then no change is reported (also will be empty)

Does that follow your expectations?

@vdcloudcraft
Copy link
Author

Yes, that would have been my expected result.

The setup below may be a solution in some cases, as it copies the entire src folder (no trailing slash in src) and does create dest.

ansible.windows.win_copy:
  remote_src: true
  src: C:\dir1\emptyDir
  dest: C:\dir2\ # does not exist yet

Unfortunately in my case I have no control over the actual name of emptyDir or its potential content (acutal values come from win_find), but need its content to be in a folder called emptyDir2.

I'm aware that I can work around this issue by ensuring dest exists using win_file: state=directory, but the documentation reads as if it should be possible with a single task. And I'd really appreciate if it did, because otherwise I'll have to start guarding against this edge case with win_file in multiple projects. :)

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