Skip to content

Instantly share code, notes, and snippets.

@gravcat
Created January 29, 2018 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravcat/09a2ba7bc8250a2ea98f937bcb528f4a to your computer and use it in GitHub Desktop.
Save gravcat/09a2ba7bc8250a2ea98f937bcb528f4a to your computer and use it in GitHub Desktop.
Use the built-in win_ping module in Ansible to confirm proper connectivity and credential success for manipulating Windows hosts
---
- hosts: windows
tasks:
- name: ping
win_ping:
@gravcat
Copy link
Author

gravcat commented Jan 29, 2018

A config file will be needed, I used ansible conventions to have it sucked in as part of the "windows" host group. In group_vars/windows.yml (relative to the playbook):

# it is suggested that these be encrypted with ansible-vault:
# ansible-vault edit group_vars/windows.yml

ansible_user: username
ansible_password: super_secret_password
ansible_port: 5986
ansible_connection: winrm
# The following is necessary for Python 2.7.9+ (or any older Python that has backported SSLContext, eg, Python 2.7.5 on RHEL7) when using default WinRM self-signed certificates:
ansible_winrm_server_cert_validation: ignore

More info on this at the Ansible Windows Support page.


Upon execution, the output should be rather concise:

PLAY [windows] *****************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [192.168.10.24]

TASK [ping] ************************************************************************************************************
ok: [192.168.10.24]

PLAY RECAP *************************************************************************************************************
192.168.10.24              : ok=2    changed=0    unreachable=0    failed=0

The ok near the Gathering Facts and ping tasks show us that the connection and execution against this machine(s) is good and well, ready to go!

@adsnjhfyeqw231eas
Copy link

adsnjhfyeqw231eas commented Feb 17, 2020

Hi @gravcat, I am new to ansible and trying to do the same task (pinging a windows client with control node: centos 7).
ansible_windows
I am not sure what is going wrong? Kindly help.

@adsnjhfyeqw231eas
Copy link

adsnjhfyeqw231eas commented Feb 17, 2020

@Gravecat In which file you are passing these?
ansible_user: username
ansible_password: super_secret_password
ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore

Is this needed to add in the ping_windows_hosts.yml you are using or elsewhere? Please take a look

@gravcat
Copy link
Author

gravcat commented Feb 17, 2020

Hey @TridevGuha! Welcome to Ansible, it's a wonderful tool.

https://docs.ansible.com/ansible/latest/user_guide/intro_getting_started.html#action-create-a-basic-inventory
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#basics

As for the basics to get this rolling in a clean way you'll need

  • a host inventory (hosts)
  • a group_vars file (group_vars/windows.yml)
  • a playbook (main.yml)

It looks like you are putting your playbook as group_vars by mistake, and ansible is trying to read it and failing. Move windows1.yml to ../main.yml

In group_vars/windows.yml, place:

ansible_user: username
ansible_password: super_secret_password
ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore

At this point, it should work :)


unrelated, here's an example of a simple playbook. Doesn't include group_vars like this one would, but shows how your main.yml playbook entrypoint should be!

https://gitlab.com/adrift/ansible/-/tree/master/update-rust

@adsnjhfyeqw231eas
Copy link

adsnjhfyeqw231eas commented Feb 18, 2020

@gravcat I have made the changes and It is working now! Thanks for the response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment