weijie lin
weijie lin

Reputation: 153

Ansible to copy files between windows box using UNC path

I'm trying to use ansible to install ODBC driver, which is stored in a share folder. However, it seems like the leading slash "\\" cause some parsing issue and Ansible couldn't find the file. I'm wondering is there any work around to perform such task. I have no problems to execute the copy command on the target windows box and there shouldn't be any permission issues.

Playbook:

---
# This play-book is part of VM Checkout
# This job will install SQL ODBC Driver
# This job depends on access to \\company\software\Utilities
- name: Install SQL ODBC Driver From Microsoft
  hosts: '{{ remote_host }}'
  tasks:
  - name: Fetch ODBC Driver From Share
    win_shell: Copy-Item "\\Company\\us410_software\\Utilities\\msodbcsql_x64.msi" D:\Software

  - name: Install ODBC Driver
    win_msi:
      path: D:\Software\msodbcsql_x64.msi
      wait: yes

The error I'm getting:

"changed": true,
    "cmd": "Copy-Item \"\\\\Company\\\\us410_software\\\\Utilities\\\\msodbcsql_x64.msi\" D:\\Software",
    "delta": "0:00:01.368157",
    "end": "2017-08-14 08:25:41.869527",
    "failed": true,
    "rc": 1,
    "start": "2017-08-14 08:25:40.501370",
    "stderr": "Copy-Item : Access is denied\r\nAt line:1 char:65\r\n+ [Console]::InputEncoding = New-Object Text.UTF8Encoding $false; Copy-Item \r\n\"\\\\820 ...\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n~~~\r\n    + CategoryInfo          : PermissionDenied: (\\\\Company\\...odbcsql_x005F_x64 \r\n   .msi:String) [Copy-Item], UnauthorizedAccessException\r\n    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.Powe \r\n   rShell.Commands.CopyItemCommand\r\n \r\nCopy-Item : Cannot find path \r\n'\\\\Company\\\\us410_software\\\\Utilities\\\\msodbcsql_x005F_x64.msi' because it does \r\nnot exist.\r\nAt line:1 char:65\r\n+ [Console]::InputEncoding = New-Object Text.UTF8Encoding $false; Copy-Item 

Upvotes: 2

Views: 10991

Answers (4)

Vaseem Ahamad
Vaseem Ahamad

Reputation: 1

In YAML files, backslashes are escape characters, so you might need to double them when specifying UNC paths.

Check below image or link for more info and it will work 100 %

enter image description here

Upvotes: 0

Emmanuel Osimosu
Emmanuel Osimosu

Reputation: 6004

It possible you are getting into a second hop authentication scenario. There are ways around this (either by using a domain user or credssp).

If you are already connecting as a domain user, make sure you are using pywinrm==0.2.0 or later, and add ansible_winrm_kerberos_delegation=true to the inventory vars for the Windows host in question.

If CredSSP is an option for you, follow the instructions here: https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html#credssp.

Upvotes: 1

user3479650
user3479650

Reputation: 41

Try using become parameter, it worked for me.

- name: Fetch ODBC Driver From Share
    win_shell: Copy-Item "\\Company\\us410_software\\Utilities\\msodbcsql_x64.msi" D:\Software
    become: yes
    become_method: runas
    become_user: domain\user_id
    vars:
      ansible_become_password: "ChangePassword"

Upvotes: 4

Kevin Rivers
Kevin Rivers

Reputation: 21

I think you might just need to correct your input to

win_shell: Copy-Item 
"\\\\\\\\Company\\\\us410_software\\\\Utilities\\\\msodbcsql_x64.msi" 
D:\Software

The slash is a special character inside quotes in yaml and double slashes get converted into a single one so you need 4 of them to denote the start of a typical UNC path in yaml.

If that doesn't solve it, then I suspect you are using Kerberos authentication to access your Windows machines from Ansible and hitting the double hop problem. I struggled with this same problem myself for many days until I found this http://www.absolutejam.co.uk/blog/ansible-windows-credssp/ and switched to CredSSP. That solved my issue and all the built-in Ansible modules worked great after. No issues using UNC paths in modules like win_copy and win_file anymore.

Follow the directions in the above link to ready the Windows machines for CredSSP then use the Ansible documentation here http://docs.ansible.com/ansible/latest/intro_windows.html#credssp to enable it on the server.

Upvotes: 1

Related Questions