# specifying targets for win\_psexec module?

**URL:** <https://forum.ansible.com/t/specifying-targets-for-win-psexec-module/25559>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [September 21, 2017, 4:15pm UTC](https://forum.ansible.com/t/specifying-targets-for-win-psexec-module/25559 "2017-09-21T16:15:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Kersley](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@Steve\_Kersley](https://forum.ansible.com/u/Steve_Kersley)\
**Post date:** [September 21, 2017, 4:15pm UTC](https://forum.ansible.com/t/specifying-targets-for-win-psexec-module/25559/1 "2017-09-21T16:15:19Z")

</div>

As I understand it, the win\_psexec action connects to a Windows host via winrm and then uses that to run commands on a second Windows host via psexec?

My question is how to write a playbook such that the first (proxy) host is always constant, but the second hosts are the ones defined in the inventory and/or filtered on the command line (not listed in the playbook/role/variables).  
i.e. I want to run a playbook against, say, 10 hosts. Each time the win\_psexec action runs, I want it to connect to ‘proxyserver’ and use that to run remote commands on the 10 hosts, but without defining those hosts in the playbook as otherwise they’d keep needing to be changed.

(What I’m actually trying to do is to have a playbook to actually install powershell3+ and run a script to configure winrm access to allow direct ansible connection without having to go round and do that on every single server by hand, so if there’s another way to do that - or what I’m wanting to do simply can’t be done via win\_psexec then would be grateful to hear that also!)

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [September 21, 2017, 10:18pm UTC](https://forum.ansible.com/t/specifying-targets-for-win-psexec-module/25559/2 "2017-09-21T22:18:54Z")

</div>

You can have multiple plays in a playbook so your playbook could look something like this.

`

- name: setup PS requirements on all hosts  
hosts: firsthost  
tasks:

- name: install psexec  
win\_chocolatey:  
name: sysinternal  
state: present

- name: setup PS with psexec  
win\_psexec:  
command: powershell.exe -File C:\temp\upgrade\_script.ps1  
hostnames: ‘{{groups[‘windows’]}}’ # may need to find a way to exclude the current host, would need to play around with this  
username: username  
password: password  
priority: high

- name: run Ansible on all hosts  
hosts: windows  
tasks:

- name: wait for the WinRM connection to come online  
wait\_for\_connection:

- name: run whatever you want  
win\_ping:  
`

I haven’t actually used this module before but I believe it is just a wrapper around the PSExec executable so it should work. Your upgrade script would need to handle the idempotency around skipping if PS is already v3.0 and also to setup the WinRM listeners. In the end if these are new servers you are provisioning this stuff should be done in the bootstrapping/imaging process but if they are existing servers then you don’t have too much choice.

Hopefully this helps.

Thanks

Jordan
