Skip to content

Instantly share code, notes, and snippets.

@efops
Last active April 8, 2020 05:59
Show Gist options
  • Save efops/d1f9095eb609f6cbb936f5c33f26623b to your computer and use it in GitHub Desktop.
Save efops/d1f9095eb609f6cbb936f5c33f26623b to your computer and use it in GitHub Desktop.
ansible win_rm
#### Commands to enable and config win_rm in remote windows machine
### should be run as administrator in powershell
winrm quickconfig -q
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="1024"}'
winrm set winrm/config '@{MaxTimeoutms="1800000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/client '@{TrustedHosts="host1, host2, host3"}'
[wintel]
192.168.1.11 ### your remote windows hots
[wintel:vars]
ansible_user=admin ### ansible user to connect
ansible_password=admin ### ansible password to connect
ansible_port=5985 ### winrm port on remote windows machine
ansible_connection=winrm
#ansible_winrm_transport=kerberos ### for kerberos authentication
ansible_winrm_server_cert_validation=ignore ### certificate validation.

In order to connect remote windows machine from linux we should install and setup winrm ansible module.

First of all as the ansible engine doesnt have winrm module by default on linux, we should install Pywinrm module manually on linux machine.

-pip install pywinrm

Then we should enable winrm in remote windows machine. (we also could use other winrm commands writen in above in order to enable connection and other settings.)

-winrm quickconfig -q

after that we could check connection from our local linux machine to remote windows machine based on our inventory file.

-ansible all -i inventory -m win_ping

You should get a message like this :

192.168.1.11 | SUCCESS { "changed" : false, "ping" : "pong" }

That's All.

#### From ansible official documentation
- name: Copy a single file
win_copy:
src: /srv/myfiles/foo.conf
dest: c:\Temp\renamed-foo.conf
- name: Copy a single file keeping the filename
win_copy:
src: /src/myfiles/foo.conf
dest: c:\temp\
- name: Copy folder to c:\temp (results in C:\Temp\temp_files)
win_copy:
src: files/temp_files
dest: c:\Temp
- name: Copy folder contents recursively
win_copy:
src: files/temp_files/
dest: c:\Temp
- name: Copy a single file where the source is on the remote host
win_copy:
src: C:\temp\foo.txt
dest: C:\ansible\foo.txt
remote_src: True
- name: Copy a folder recursively where the source is on the remote host
win_copy:
src: C:\temp
dest: C:\ansible
remote_src: True
- name: Set the contents of a file
win_copy:
dest: C:\temp\foo.txt
content: abc123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment