# Ansible with Windows hosts

**URL:** <https://forum.ansible.com/t/ansible-with-windows-hosts/30415>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [May 24, 2019, 4:03pm UTC](https://forum.ansible.com/t/ansible-with-windows-hosts/30415 "2019-05-24T16:03:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [May 24, 2019, 4:03pm UTC](https://forum.ansible.com/t/ansible-with-windows-hosts/30415/1 "2019-05-24T16:03:05Z")

</div>

Hey,

I would like to know if there is only one solution for that ansible communicate with Windows host ?

winrm and script powershell execute on Windows host ==\> is the only solution for that ansible can communicate with Windows host ?

Because if i must configure all Windows host at hand, how do if i have 100 Windows server ?

There is one solution more simple for that ansible can communicate with Windows host please ? I want execute script sql on MSSQL on Windows Server 2012 r2 on port 1433

Thanks for your answer guy !! 🙂

Regards,

---

<div class="post-metadata">

**Author:** ![Stefano\_Leandro](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stefano\_Leandro](https://forum.ansible.com/u/Stefano_Leandro)\
**Post date:** [May 24, 2019, 4:26pm UTC](https://forum.ansible.com/t/ansible-with-windows-hosts/30415/2 "2019-05-24T16:26:06Z")

</div>

Hi ,  
Personally im using SSH ,installed from chocolatey repository.  
Since Ansible 2.8 ,ansible start to support SSH on Windows comunication …

U have to dowload ansible 2.8 from github .It’s released some days ago 🙂

Stefano

---

<div class="post-metadata">

**Author:** ![Igor\_Turovsky](https://avatars.discourse-cdn.com/v4/letter/i/cdc98d/32.png) [@Igor\_Turovsky](https://forum.ansible.com/u/Igor_Turovsky)\
**Post date:** [May 25, 2019, 9:58am UTC](https://forum.ansible.com/t/ansible-with-windows-hosts/30415/3 "2019-05-25T09:58:44Z")

</div>

Hi Ryad,

Ansible indeed commuicates wth Windows via WinRM as standard Windows protocol for remote management.  
Latest version of Ansible and latest version of Windows also support communication via SSH.

If you need to execute some SQL script on MS SQL, you have 2 directions:

1. (simler for Windows guy). Use WinRM connection from Ansible to Windows Server where MS SQL is installed.  
Copy SQL script from Ansible host to Windows with win\_copy module ([https://docs.ansible.com/ansible/latest/modules/win\_copy\_module.html](https://docs.ansible.com/ansible/latest/modules/win_copy_module.html)).  
Run sqlcmd with win\_command module ([https://docs.ansible.com/ansible/latest/modules/win\_command\_module.html](https://docs.ansible.com/ansible/latest/modules/win_command_module.html)) or win\_shell ([https://docs.ansible.com/ansible/latest/modules/win\_shell\_module.html](https://docs.ansible.com/ansible/latest/modules/win_shell_module.html)) with Invoke-Sqlcmd cmdlet passing sql script as parameter.  
Since you are using WinRM you will hit double hop issue (i.e. your sql script will be running in context of local user and authentication will fail on connection to MS SQL since it will not recognize the user running the script - see see [https://docs.ansible.com/ansible/latest/user\_guide/windows\_winrm.html#limitations](https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html#limitations)). Workarounds are:

- use become to become another user with permission to execute this script. ([https://docs.ansible.com/ansible/latest/user\_guide/become.html#become-and-windows](https://docs.ansible.com/ansible/latest/user_guide/become.html#become-and-windows))
- use credssp
- use Kerberos delegation

There methods will help in case you are using Windows only authentiation on SQL side.  
If you want to use sql login, you supply alternate credentials in sqlcmd or invoke-sqlcmd in case you are using sql logins.

This would be rather quick way to achieve what you need though win\_shell and win\_command are anti-patterns for Ansible’s declarative approach.

1. (simpler for Python guy) Use delegation and run query from Ansible host. You will have to create some python code (ideally in form of Ansible module) what will connect to SQL from Ansible controller and execute the script. In this case you will connect from Ansible host to MS SQL via 1433/tcp.  
AFAIK, there is no ready to use module to run SQL scripts on MS SQL from Ansible, but there is module for database creation/deletion, you may want to take a look on it (doc - [https://docs.ansible.com/ansible/latest/modules/mssql\_db\_module.html#mssql-db-module](https://docs.ansible.com/ansible/latest/modules/mssql_db_module.html#mssql-db-module), repo - [https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/database/mssql/mssql\_db.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/database/mssql/mssql_db.py)).

Thank you,

- Igor

пт, 24 мая 2019 г. в 19:26, Stefano Leandro \<[mr.stefanoleandro@gmail.com](mailto:mr.stefanoleandro@gmail.com)\>:

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [June 3, 2019, 9:42am UTC](https://forum.ansible.com/t/ansible-with-windows-hosts/30415/4 "2019-06-03T09:42:25Z")

</div>

Hi,

Thanks for answers community ansible !! 😉

Regards,

---

<div class="post-metadata">

**Author:** ![Igor\_Turovsky](https://avatars.discourse-cdn.com/v4/letter/i/cdc98d/32.png) [@Igor\_Turovsky](https://forum.ansible.com/u/Igor_Turovsky)\
**Post date:** [June 3, 2019, 10:11am UTC](https://forum.ansible.com/t/ansible-with-windows-hosts/30415/5 "2019-06-03T10:11:06Z")

</div>

Actually, there is one more option in case your servers already have Powershell 5 - you can use Ansible’s win\_dsc module and this resource [https://github.com/PowerShell/SqlServerDsc/blob/dev/README.md#sqlscript](https://github.com/PowerShell/SqlServerDsc/blob/dev/README.md#sqlscript) from sql server dsc.

Thank you,

Igor

Thank you,

- Igor

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [June 4, 2019, 2:50pm UTC](https://forum.ansible.com/t/ansible-with-windows-hosts/30415/6 "2019-06-04T14:50:32Z")

</div>

Hi Igor,

Thank you very much for your answer !! 🙂

I will try this issue 😉

Regards,
