by Anish
Posted on Tuesday June 25, 2019
Ansible Windows module uses winrm connection, in order to execute commands in the Windows machine. There are many module supported by the ansible
Let's setup a BASIC ping-pong using win_ping module
This is the directory struture we are going to build
|--group_vars
|------all.yml
|--hosts
|--test.yml
The all.yml file in the group_vars directory will hold necessary credentials for Windows Authentication. When setting up the inventory, the following variables are required:
---
ansible_ssh_user: "Administrator"
ansible_ssh_pass: "Administrator"
ansible_ssh_port: "5985"
ansible_connection: "winrm"
ansible_winrm_server_cert_validation: ignore
The test.yml is the ansible-playbook which is using win_ping module to. A windows version of the classic ping module
---
- hosts: win
tasks:
- name: Ping Windows Hosts
win_ping:
Run the Playbook
$ ansible-playbook test.yml -i hosts
PLAY [win] **********************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [10.65.148.49]
TASK [Ping Windows Hosts] *******************************************************************************************************************************************
ok: [10.65.148.49]
PLAY RECAP **********************************************************************************************************************************************************
10.65.148.49 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
We tested sucessfully ping command, now jump to another ansible windows module win_shell which will help to execute any command in windows from the controller node.
Here is the sample ansible-playbook for win_shell.yml
---
- hosts: win
tasks:
- name: Execute command in Windows
win_shell: dir
args:
executable: cmd
register: out
- debug: var=out.stdout_lines
Run the playbook.
$ ansible-playbook win_shell.yml -i hosts
PLAY [win] **********************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [10.65.148.49]
TASK [Execute command in Windows] ***********************************************************************************************************************************
changed: [10.65.148.49]
TASK [debug] ********************************************************************************************************************************************************
ok: [10.65.148.49] => {
"out.stdout_lines": [
" Volume in drive C has no label.",
" Volume Serial Number is D0D0-E635",
"",
" Directory of C:\\Users\\Administrator",
"",
"06/24/2019 04:03 AM <DIR> .",
"06/24/2019 04:03 AM <DIR> ..",
"06/17/2019 08:45 PM <DIR> Contacts",
"06/17/2019 08:45 PM <DIR> Desktop",
"06/17/2019 08:45 PM <DIR> Documents",
"06/17/2019 08:45 PM <DIR> Downloads",
"06/17/2019 08:45 PM <DIR> Favorites",
"06/17/2019 08:45 PM <DIR> Links",
"06/17/2019 08:45 PM <DIR> Music",
"06/17/2019 08:45 PM <DIR> Pictures",
"06/17/2019 08:45 PM <DIR> Saved Games",
"06/17/2019 08:45 PM <DIR> Searches",
"06/17/2019 08:45 PM <DIR> Videos",
" 0 File(s) 0 bytes",
" 13 Dir(s) 64,714,444,800 bytes free"
]
}
PLAY RECAP **********************************************************************************************************************************************************
10.65.148.49 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Thanku for reading !!! Give a Share for Support
Asking for donation sound bad to me, so i'm raising fund from by offering all my Nine book for just $9