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

Module fetch fails on windows host with wildcards in filename #73128

Closed
amhn opened this issue Jan 6, 2021 · 4 comments
Closed

Module fetch fails on windows host with wildcards in filename #73128

amhn opened this issue Jan 6, 2021 · 4 comments
Labels
affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. traceback This issue/PR includes a traceback. windows Windows community

Comments

@amhn
Copy link

amhn commented Jan 6, 2021

SUMMARY

Using fetch to get files from windows hosts fails if the filename can be interpreted as a powershell wildcard. For example "new[12].txt"

ISSUE TYPE
  • Bug Report
COMPONENT NAME

connection winrm.py

ANSIBLE VERSION
ansible 2.10.4
  config file = /home/aniess/ansible/inhouse/ansible.cfg
  configured module search path = ['/home/aniess/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/aniess/.local/lib/python3.6/site-packages/ansible
  executable location = /home/aniess/.local/bin/ansible
  python version = 3.6.9 (default, Oct  8 2020, 12:12:24) [GCC 8.4.0]
CONFIGURATION
CACHE_PLUGIN(/home/aniess/ansible/inhouse/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/home/aniess/ansible/inhouse/ansible.cfg) = ./tmp
DEFAULT_GATHERING(/home/aniess/ansible/inhouse/ansible.cfg) = smart
DEFAULT_HOST_LIST(/home/aniess/ansible/inhouse/ansible.cfg) = ['/home/aniess/ansible/inhouse/inventory/inventory.yml']
DEFAULT_STDOUT_CALLBACK(/home/aniess/ansible/inhouse/ansible.cfg) = yaml
DEFAULT_VAULT_PASSWORD_FILE(/home/aniess/ansible/inhouse/ansible.cfg) = /home/aniess/ansible/inhouse/pw_vault_prod
DISPLAY_ARGS_TO_STDOUT(/home/aniess/ansible/inhouse/ansible.cfg) = True
OS / ENVIRONMENT

Ansible host is Ubuntu 18.04. Target is Windows Server 2019 (1809)

STEPS TO REPRODUCE

Create File named "C:\www\new[12].txt" on Windows host and run task below

  - name: Fetch test
    fetch:
      src: C:\www\new[12].txt
      dest: test
EXPECTED RESULTS

File is retrieved from Windows host

ACTUAL RESULTS

Ansible aborts with the error below.

TASK [Fetch test src=C:\www\new[12].txt, dest=test] *********************************************************************************************************************
Traceback (most recent call last):
  File "/home/aniess/.local/lib/python3.6/site-packages/ansible/plugins/connection/winrm.py", line 682, in fetch_file
    raise IOError(to_native(result.std_err))
OSError: #< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj><Obj S="progress" RefId="1"><TNRef RefId="0" /><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj><S S="Error">Set-StrictMode -Version Latest_x000D__x000A_</S><S S="Error">$path = 'C:\www\new[12].txt'_x000D__x000A_</S><S S="Error">If (Test-Path -Path $path -PathType Leaf)_x000D__x000A_</S><S S="Error">{_x000D__x000A_</S><S S="Error">$buffer_size = 524288_x000D__x000A_</S><S S="Error">$offset = 0_x000D__x000A_</S><S S="Error">$stream = New-Object -TypeName IO.FileStream($path, [IO.FileMode]::Open, [IO.FileAccess]::Read, _x000D__x000A_</S><S S="Error">[IO.FileShare]::ReadWrite)_x000D__x000A_</S><S S="Error">$stream.Seek($offset, [System.IO.SeekOrigin]::Begin) &gt; $null_x000D__x000A_</S><S S="Error">$buffer = New-Object -TypeName byte[] $buffer_size_x000D__x000A_</S><S S="Error">$bytes_read = $stream.Read($buffer, 0, $buffer_size)_x000D__x000A_</S><S S="Error">if ($bytes_read -gt 0) {_x000D__x000A_</S><S S="Error">$bytes = $buffer[0..($bytes_read - 1)]_x000D__x000A_</S><S S="Error">[System.Convert]::ToBase64String($bytes)_x000D__x000A_</S><S S="Error">}_x000D__x000A_</S><S S="Error">$stream.Close() &gt; $null_x000D__x000A_</S><S S="Error">}_x000D__x000A_</S><S S="Error">ElseIf (Test-Path -Path $path -PathType Container)_x000D__x000A_</S><S S="Error">{_x000D__x000A_</S><S S="Error">Write-Host "[DIR]";_x000D__x000A_</S><S S="Error">}_x000D__x000A_</S><S S="Error">Else_x000D__x000A_</S><S S="Error">{_x000D__x000A_</S><S S="Error">Write-Error "$path does not exist";_x000D__x000A_</S><S S="Error">Exit 1;_x000D__x000A_</S><S S="Error">} : C:\www\new[12].txt does not exist_x000D__x000A_</S><S S="Error">    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException_x000D__x000A_</S><S S="Error">    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S></Objs>
fatal: [www]: FAILED! => 
  msg: failed to transfer file to "/home/aniess/ansible/inhouse/test/www/C:/www/new[12].txt"
FIX

Using -LiteralPath to prevent Test-Path from evaluating wildcards fixes the issue for me:

diff --git a/lib/ansible/plugins/connection/winrm.py b/lib/ansible/plugins/connection/winrm.py
index 6ab6ca7bc4..85de3fad2e 100644
--- a/lib/ansible/plugins/connection/winrm.py
+++ b/lib/ansible/plugins/connection/winrm.py
@@ -646,7 +646,7 @@ class Connection(ConnectionBase):
                 try:
                     script = '''
                         $path = "%(path)s"
-                        If (Test-Path -Path $path -PathType Leaf)
+                        If (Test-Path -LiteralPath $path -PathType Leaf)
                         {
                             $buffer_size = %(buffer_size)d
                             $offset = %(offset)d
@ansibot
Copy link
Contributor

ansibot commented Jan 6, 2021

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. traceback This issue/PR includes a traceback. windows Windows community labels Jan 6, 2021
@sivel
Copy link
Member

sivel commented Jan 12, 2021

I think we would need to verify whether this is handled on *nix via SSH as well. If it doesn't, we probably shouldn't do this for Windows.

@sivel sivel removed the needs_triage Needs a first human triage before being processed. label Jan 12, 2021
@amhn
Copy link
Author

amhn commented Jan 12, 2021

- name: Test
  fetch:
    src: /home/andy/new[12].txt
    dest: test

Works as expected:

% ansible-playbook test.yml

PLAY [Test playbook] *************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************
ok: [www]

TASK [Test] ************************************************************************************************************************************
changed: [www]

PLAY RECAP **********************************************************************************************************************************
www           : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

@ansibot ansibot added the has_pr This issue has an associated PR. label Nov 26, 2021
@mattclay
Copy link
Member

mattclay commented Aug 9, 2023

Fixed in #74723

@mattclay mattclay closed this as completed Aug 9, 2023
@ansible ansible locked and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. traceback This issue/PR includes a traceback. windows Windows community
Projects
None yet
Development

No branches or pull requests

4 participants