# Suggestions for win\_copy and win\_ansible

**URL:** <https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [October 13, 2014, 10:06pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495 "2014-10-13T22:06:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [October 13, 2014, 10:06pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/1 "2014-10-13T22:06:41Z")

</div>

There’s been a lot of talk about these, and its time to get them into source.

As several (I can’t remember the names right now, sorry!) in this group have tested, the copy module will work (albeit with limited functionality) as long as there’s a copy.ps1 module in the windows directory, and that file is able to lay down the input file onto disk.

My suggestion is that we build win\_copy and win\_template modules, in order to reflect the differences around permissions on NTFS vs linux filesystems.  
From what I can see, Ansible uses a 1:1 mapping between action plugins and modules, which would mean that we would need a win\_copy.py and win\_template.py action plugin as well.

The win\_copy.py action plugin would be 100% identical to its copy.py sibling, and the win\_template.py plugin would be identical to the template.py plugin with the only difference being that it calls win\_copy instead of copy when executing the file copy operation(s) underneath.

my suggestion is that we build out simple support for copying/templating files using the matching modules, since the lack of these functions are limiting the usefullness of Ansible on the Windows platform. Once we have those in source we can start building out support for permissions etc.

I am more than willing to do this tweaking, but I’d like Michael (or someone’s) take on this path before i mash together (or steal) the code.

What say ye?

-Trond

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [October 14, 2014, 4:05am UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/2 "2014-10-14T04:05:03Z")

</div>

Given the differences in specifying permissions and no need for some of the  
other file/copy options, I'm +1 for having separate  
win\_file/win\_copy/win\_template modules.

So, to add to your notes, I think the following changes are needed to get  
all of this working. I'm currently tackling some kerberos-related things,  
but would gladly offer my feedback if you wanted to submit a PR or point me  
to a branch of work in progress.

In ansible:

&nbsp;&nbsp;&nbsp;- lib/ansible/runner/action\_plugins/win\_copy.py would be mostly based on  
&nbsp;&nbsp;&nbsp;copy.py.  
&nbsp;&nbsp;&nbsp;- lib/ansible/runner/action\_plugins/win\_template.py would be mostly  
&nbsp;&nbsp;&nbsp;based on template.py. Somebody with initials CC should probably fix this  
&nbsp;&nbsp;&nbsp;todo while we're at it:  
&nbsp;&nbsp;&nbsp;[https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/action\_plugins/template.py#L79](https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/action_plugins/template.py#L79)  
&nbsp;&nbsp;&nbsp;- lib/ansible/module\_utils/powershell.ps1 - Add common functions (MD5,  
&nbsp;&nbsp;&nbsp;etc.) needed for win\_file and win\_copy modules.  
&nbsp;&nbsp;&nbsp;- test/integration/... - Integration tests for  
&nbsp;&nbsp;&nbsp;win\_file/win\_copy/win\_template.

In ansible-modules-core:

&nbsp;&nbsp;&nbsp;- windows/win\_file.ps1 could be based on  
&nbsp;&nbsp;&nbsp;[https://gist.github.com/tkinz27/8a0b49f3c8b0ea4575c6](https://gist.github.com/tkinz27/8a0b49f3c8b0ea4575c6) without the  
&nbsp;&nbsp;&nbsp;permissions code. I'd think only the path and state parameters are needed;  
&nbsp;&nbsp;&nbsp;state only needs to support file, directory, touch and absent.  
&nbsp;&nbsp;&nbsp;- windows/win\_file.py would define module docs.  
&nbsp;&nbsp;&nbsp;- windows/win\_copy.ps1 could be based on  
&nbsp;&nbsp;&nbsp;[https://gist.github.com/tkinz27/fd92ba9af0e0309614ee](https://gist.github.com/tkinz27/fd92ba9af0e0309614ee) but use PowerShell  
&nbsp;&nbsp;&nbsp;3 compatible MD5 code from  
&nbsp;&nbsp;&nbsp;[https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/shell\_plugins/powershell.py#L87](https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/shell_plugins/powershell.py#L87)  
&nbsp;&nbsp;&nbsp;or use a common function for MD5.  
&nbsp;&nbsp;&nbsp;- windows/win\_copy.py would define module docs.  
&nbsp;&nbsp;&nbsp;- windows/win\_template.py would define module docs.

The permissions code from  
[https://gist.github.com/tkinz27/8a0b49f3c8b0ea4575c6](https://gist.github.com/tkinz27/8a0b49f3c8b0ea4575c6) could probably become  
the basis for a future win\_acl module or be added to  
win\_file/win\_copy/win\_template in the future.

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [October 14, 2014, 7:00am UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/3 "2014-10-14T07:00:39Z")

</div>

Great stuff! I think we’re on the same page here.

There are some problems I see with my win\_template (copied from template and changed the referenced command from copy to win\_copy). It seems to work nicely if the dest file doesn’t already exist, but if it the dest file exists it throws some weird error saying it’s missing some core modules. I’ll try and branch out my changed files so that someone in the know can have a look.

In the meantime, I’ll get going with win\_copy and win\_template powershell scripts, making sure to test them on PS v3.

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [October 14, 2014, 4:08pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/4 "2014-10-14T16:08:10Z")

</div>

I’ve started working on this at:  
[https://github.com/trondhindenes/ansible/tree/win\_copy\_win\_template](https://github.com/trondhindenes/ansible/tree/win_copy_win_template)  
and  
[https://github.com/trondhindenes/ansible-modules-core/tree/win\_copy\_win\_template](https://github.com/trondhindenes/ansible-modules-core/tree/win_copy_win_template)

Appreciating any help!  
-Trond

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 19, 2014, 8:08pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/5 "2014-10-19T20:08:03Z")

</div>

Thanks for this I hope to get some testing time this week. If I do I will give these a try - completely agree on the need for these.

Best

Jon

---

<div class="post-metadata">

**Author:** ![John\_Jelinek](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@John\_Jelinek](https://forum.ansible.com/u/John_Jelinek)\
**Post date:** [November 8, 2014, 1:57am UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/6 "2014-11-08T01:57:25Z")

</div>

Has there been any progress on this?

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [November 8, 2014, 3:22pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/7 "2014-11-08T15:22:30Z")

</div>

A little. I tried out Trond’s changes and sent him some revisions last week. I had a functioning but incomplete win\_copy module, but no tests yet and haven’t actually tried the win\_template functionality.

I’m hoping to do a bit more on this as I get time as I have a functioning win\_unzip module which I want to contribute. It works well but cannot yet copy files from the controller to the host.

Jon

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://forum.ansible.com/u/Joey)\
**Post date:** [November 19, 2014, 7:02pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/8 "2014-11-19T19:02:16Z")

</div>

Trying out win\_copy and it looks like Get-FileHash requires Powershell 4. Do the configure\_remoting and upgrade\_powershell scripts need to be updated to require Powershell 4? Or is there another way of doing the file hash that doesn’t require updating?

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://forum.ansible.com/u/Joey)\
**Post date:** [November 19, 2014, 7:18pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/9 "2014-11-19T19:18:01Z")

</div>

Answering my own question, this seems to work.

```auto
function md5hash($path)
{
    $fullPath = Resolve-Path $path
    $md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
    $file = [System.IO.File]::Open($fullPath,[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
    [System.BitConverter]::ToString($md5.ComputeHash($file))
    $file.Dispose()
}

```

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://forum.ansible.com/u/Joey)\
**Post date:** [November 19, 2014, 7:34pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/10 "2014-11-19T19:34:34Z")

</div>

After switching to the new md5 hash function, win\_copy is partially working for me. If the md5 match fails and it has to write a new file, it works, but breaks most whitespace in the file (specifically it seems to remove linebreaks and tabs).

If the md5 match succeeds and it doesn’t have to write the file, there is a WINRM error and it fails. The error looks like the WINRM PUT command are writing out to ansible-tmp- **\file, and the WINRM EXEC command is looking for ansible-tmp-** \file.ps1 (with the extension), so it errors out.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [November 19, 2014, 9:10pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/11 "2014-11-19T21:10:40Z")

</div>

This is probably because behind the scenes it is attempting to use the file module to get info about the file if the md5 match succeeds.

I have been making progress on implementing win\_copy, win\_template and win\_file and hope to have a PR to share very shortly - I will also include integration tests which I have passing for win\_copy and win\_file right now.

Jon

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [November 19, 2014, 10:40pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/12 "2014-11-19T22:40:00Z")

</div>

Very nice, thanks for your work on this!

Regarding the winrm connection adding a .ps1 extension, I may be able to  
remove that part entirely if we officially require windows modules to have  
the .ps1 extension. At the moment, it still searches for module.ps1 then  
module (no extension).

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [November 23, 2014, 7:41pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/13 "2014-11-23T19:41:40Z")

</div>

Sorry this is taking longer than I hoped. I am debugging my through the consequences of the changes around md5 and checksum logic at the moment. With a bit of luck and a couple of clear hours I should have a PR to share. Just hope I can get something in before 1.8 sails.

Jon

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [November 24, 2014, 9:08pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/14 "2014-11-24T21:08:41Z")

</div>

Just to say at long last I have created PRs to add win\_copy, win\_file and win\_template modules.

Both PRs would be needed (the modules all need action\_plugins, which are in the main ansible project).

If anyone has the chance to review these and try them out I would be very grateful. Right now I don’t have a windows server 2008 /2008 r2 machine I can run against so if any one could try the integration tests against that, that would be really helpful.

The PRs are:

[https://github.com/ansible/ansible/pull/9611](https://github.com/ansible/ansible/pull/9611)

[https://github.com/ansible/ansible-modules-core/pull/384](https://github.com/ansible/ansible-modules-core/pull/384)

All the best,

Jon

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://forum.ansible.com/u/Joey)\
**Post date:** [November 24, 2014, 10:22pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/15 "2014-11-24T22:22:56Z")

</div>

How do I run the integration tests? I’m trying out win\_template but it does the checksum and decides it doesn’t need to do anything even though it hasn’t updated the file. I’m trying to find the flag that keeps debug files on the client so I can look at the temp ps1 files and figure out why.

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://forum.ansible.com/u/Joey)\
**Post date:** [November 24, 2014, 10:33pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/16 "2014-11-24T22:33:03Z")

</div>

Trying to run the script in PowerShell ISE directly on the server gives this error:

`

PowerShell : Cannot find an overload for “CompareTo” and the argument count: “1”.  
At line:1 char:1

- PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File "C:\Us …
- 

```auto

```

- CategoryInfo : NotSpecified: (Cannot find an …ent count: “1”.:String) , RemoteException
- FullyQualifiedErrorId : NativeCommandError

At C:\Users\localadmin\AppData\Local\Temp\ansible-tmp-1416867885.79-1510893868  
84966\win\_copy.ps1:223 char:9

- If (! $src\_md5.CompareTo($dest\_md5))
- 

```auto

```

- CategoryInfo : NotSpecified: (🙂 , MethodException
- FullyQualifiedErrorId : MethodCountCouldNotFindBest

Cannot find an overload for “CompareTo” and the argument count: “1”.  
At C:\Users\localadmin\AppData\Local\Temp\ansible-tmp-1416867885.79-1510893868  
84966\win\_copy.ps1:230 char:10

- If ( $src\_md5.CompareTo($dest\_md5))
- 

```auto

```

- CategoryInfo : NotSpecified: (🙂 , MethodException
- FullyQualifiedErrorId : MethodCountCouldNotFindBest

{  
“changed”: false,  
“checksum”: [  
“3688374325b992def12793500307566d”,  
“”  
]  
}

`

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [November 24, 2014, 10:37pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/17 "2014-11-24T22:37:30Z")

</div>

> 

I use --start-at-task or temporarily add my own tags if I’m trying to work out an issue with one particular task.

make test\_win already adds -v, so you can specify -vvvv and you’ll be able to see the full stdout/stderr returned from trying to run the module.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [November 25, 2014, 11:22pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/18 "2014-11-25T23:22:33Z")

</div>

Thanks for this, I’d add that in this case I wouldn’t recommend running win\_copy win\_file or win\_template without the corresponding action plugins as the plugins are needed to

That said, I do debug powershell by setting ANSIBLE\_KEEP\_REMOTE\_FILES=1 and then running the module.ps1 from inside Powershell ISE after a playbook run has completed or failed. One little issue to be aware of is you need to pass the name of the arguments file in without the .\ that powershell likes to put in place if you use tab completion.

So run  
.\win\_copy.ps1 arguments  
not  
.\win\_copy.ps1 .\arguments

Jon

---

<div class="post-metadata">

**Author:** ![Janaka\_Abeywardhana](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@Janaka\_Abeywardhana](https://forum.ansible.com/u/Janaka_Abeywardhana)\
**Post date:** [December 5, 2014, 7:20pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/19 "2014-12-05T19:20:50Z")

</div>

Hi,  
What’s the latest with the win\_copy module?

Thanks

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [December 5, 2014, 7:53pm UTC](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495/20 "2014-12-05T19:53:00Z")

</div>

There are 2 pull requests (one needs a rebase):  
[https://github.com/ansible/ansible/pull/9611](https://github.com/ansible/ansible/pull/9611) and  
[https://github.com/ansible/ansible-modules-core/pull/384](https://github.com/ansible/ansible-modules-core/pull/384) and some ongoing  
discussion on implementation details:  
[https://groups.google.com/d/topic/ansible-devel/VRXSKQgEV70/discussion](https://groups.google.com/d/topic/ansible-devel/VRXSKQgEV70/discussion)

Once those few minor issues are resolved and we have a chance to review and  
test, should be merged into devel.

[Next page](https://forum.ansible.com/t/suggestions-for-win-copy-and-win-ansible/16495.md?page=2)
