How to do stuff on the remote Windows node from the command node?

Linux Command Node: CentOs 6, Ansible 2.1, Python 2.7.13

Windows Remote Nodes: Windows 7, Powershell 3.0

Encryption: SSL

So far I’ve figured out how to use these tricks in my playbook to do stuff on the remote Windows node from the command node. Are there other methods I’ve left out?

Use Ansible Modules (especially those starting with “win_”) to do something on a remote node

  • name: Copy a windows file from control node to remote node
    win_copy: src=“/etc/ansible/winFiles/getFileVersion.exe” dest=“C:/Temp/”

-name: Restart a remote node
win_reboot:

Use the Ansible “raw:” module to run commands or executables on the remote node

  • name: Issue a Windows command to run on a remote node from control node
    raw: Rename C:/temp/oldName.txt C:/temp/newName.txt

  • name: Issue a Powershell Cmdlet to run on a remote node from control node
    raw: Copy-Item “C:/source/.” -Destination “C:/destination/”

  • name: Execute windows executable previously copied to a remote node from the control node
    raw: C:/temp/getFileVersion.exe “C:/Windows/driver.dll”

Use the Ansible “script:” module to copy, run and delete a Powershell module on the remote node from the control node.

  • name: run a powershell script on a remote node from the control node
    script: /etc/ansible/psScripts/doesItemExist.ps1 “C:/TempBackup/”

Other ideas?

Here are some further tips for people reading

  • Use the yaml syntax when writting task rather than the key=value format, this makes it a lot simpler to write Windows paths.

e.g for the first task do.

`

  • name: Copy a windows file from control node to remote node
    win_copy:
    src: /etc/ansible/winFiles/getFileVersion.exe
    dest: C:\Temp\

`

  • Use backslashes for the Windows paths, if you use the yaml syntax, you only need to escape them when using double quote “”

e.g all of the below lead to the same value.

`

  • some_module:
    path: C:\temp
    quoted_path: ‘C:\temp’
    double_quoted_path: “C:\temp”
    `
  • Avoid using the raw module, use win_command or win_shell instead, you can’t use become or environment variables with raw

  • Use win_command when you are running an executable, avoid using this when running shell commands like dir, del or PowerShell cmdlets like New-Item, Copy-Item and so on

  • Use win_shell when you are running shell commands or powershell cmdlets, avoid using this to run executables as you need to deal with shell escaping for complex arguments

  • For tasks that give you an access is denied, use become to run it like it would locally (not experimental as of the 2.5 release)

`

this does not work and leads to an access is denied error

  • name: upgrade powershell
    win_chocoletey:
    name: poweshell
    state: latest

when using become this is no longer an issue

  • name: upgrade powershell
    win_chocolatey:
    name: powershell
    state: latest
    become: yes
    become_method: runas
    become_user: SYSTEM
    `
  • If there is not an Windows module to complete a task you are looking for, look to see if there is an DSC resource instead (PowerShell v5 or newer is required)

e.g. to manage Hyper-V you can use the DSC resource like so

`

  • name: install Hyper-V DSC resource
    win_psmodule:
    name: xHyper-V
    state: present

  • name: create Hyper-V VM
    win_dsc:
    resource_name: xVMHyperv # the DSC module to use

the below parameters are all the DSC parameters to set

Ensure: present
Name: HyperVHost
VhdPath: C:\temp\vm.vhd
StartupMemory: 6291456000
Generation: 2
ProcessorCount: 2
`

There are numerous other things that are good to know but you are probably best looking at the new documentation for Windows here http://docs.ansible.com/ansible/devel/windows.html.

Thanks

Jordan