Ansible 2.0 - Move file on remote Windows machine fails using raw

It appears that the only way to copy or move files on remote Windows Servers requires the use of
the raw module (or uploading your own PS script). However, I can’t get even a basic move to work.

`

  • name: Configure the Windows Server 2012 R2 instance
    hosts: ec2hosts
    vars:
    workdirectory: ‘C:\mystuff’

  • name: move config file
    raw: move /y {{ workdirectory }}\config\junk.conf {{ workdirectory }}\config\smtp.conf

`

When this executes, I get this response:

TASK [move config file] ********************************************** task path: /Users/me/build_win.yml:99 <52.xx.xx.xx> ESTABLISH WINRM CONNECTION FOR USER: Administrator on PORT 5986 TO 52.xx.xx.xx <52.xx.xx.xx> EXEC move /y C:\mystuff\config\junk.conf C:\mystuff\config\smtp.conf fatal: [52.xx.xx.xx]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_args": {"_raw_params": "move /y C:\\mystuff\\config\\junk.conf C:\\mystuff\\config\\smtp.conf"}, "module_name": "raw"}, "rc": 1, "stderr": "#< CLIXML\r\n<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><S S=\"Error\">Move-Item : A positional parameter cannot be found that accepts argument _x000D__x000A_</S><S S=\"Error\">'C:\\mystuff\\config\\smtp.conf'._x000D__x000A_</S><S S=\"Error\">At line:1 char:1_x000D__x000A_</S><S S=\"Error\">+ move /y _x000D__x000A_</S><S S=\"Error\">C:\\mystuff\\config\\smtp.conf ..._x000D__x000A_</S><S S=\"Error\">+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_x000D__x000A_</S><S S=\"Error\">~~~_x000D__x000A_</S><S S=\"Error\"> + CategoryInfo : InvalidArgument: (:) [Move-Item], ParameterBindi _x000D__x000A_</S><S S=\"Error\"> ngException_x000D__x000A_</S><S S=\"Error\"> + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell _x000D__x000A_</S><S S=\"Error\"> .Commands.MoveItemCommand_x000D__x000A_</S><S S=\"Error\"> _x000D__x000A_</S></Objs>", "stdout": "", "stdout_lines": []}

Note that if I go to the Windows Server and paste in the command:

move /y C:\mystuff\config\junk.conf C:\mystuff\config\smtp.conf

…it works fine.

The PS error message above:

Move-Item : A positional parameter cannot be found that accepts argument _x000D__x000A_

…seems to be saying there is a problem with a cr/lf ?

I’m stumped…

So, the raw module runs commands (by default) using Powershell on Windows. Powershell won’t

run DOS commands (like move and copy) without some coaxing.

To get it to work, I had to do it like this:

`

  • name: move config file
    raw: CMD /C “move /y {{ workdirectory }}\config\junk.conf {{ workdirectory }}\config\smtp.conf”
    `

Using “CMD” tells Powershell not to parse the string that comes after in quotes.

This is a pretty good discussion:

http://blogs.technet.com/b/josebda/archive/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters.aspx