Programming, Photography, Guides, Thoughts

Ansible exe

How to run interactive .exe on Windows with Ansible


Star­ting with ver­si­on 1.7, the orchestrati­on tool Ansi­ble has a sup­port for mana­ging Win­dows machi­nes. What the docu­men­tati­on does not pro­vi­de is a gui­de on how to run inter­acti­ve appli­cati­ons properly.

The problem

If you try run­ning any inter­acti­ve app using the raw or win_command modu­les, such as in the following example:

- name: Test win_command module
  hosts: windows
  tasks:
    - name: Run Windows Calculator
      win_command: "calc.exe"
      register: output
    - debug: var=output

the execu­ti­on will hang at the Run Win­dows Cal­cu­la­tor step, with no appli­cati­on run­ning. What actu­ally hap­pens is that the appli­cati­on starts in the bac­kground in a dif­fe­rent sessi­on, without dis­pla­y­ing the UI to you.

Solution

What you need to do is to get your app run­ning in the sessi­on you are obser­ving. If the­re is only one user logged in, it will be sessi­on num­ber 1. The­re are seve­ral hac­kish, long and com­pli­ca­ted ways of doing that. And the­re is one sim­ple one. You can run the app throu­gh a pro­gram called PsExec, which will run it in correct session:

  1. Down­lo­ad the PsTools pac­kage that con­ta­ins the PsExec.exe program.
  2. Extract PsExec.exe to some fol­der <WORKING_FOLDER>.
  3. Use the following pla­y­book (repla­ce <WORKING_FOLDER> with the actu­al location):
- name: Test PsExec
  hosts: windows
  tasks:
  - name: Copy PsExec
    win_copy:
      src: &lt;WORKING_FOLDER&gt;/PsExec.exe
      dest: "{{ ansible_user_dir }}/Desktop/PsExec.exe"
      force: no

  - name: Run Windows Calculator
    win_command: "{{ ansible_user_dir }}/Desktop/psexec.exe -accepteula -nobanner -i 1 -s calc.exe"
    register: output
  - debug: var=output

The -i 1 opti­on will cause run­ning the app in sessi­on 1. The -s opti­on will run the app as sys­tem. This is not requi­red for run­ning a Win­dows Cal­cu­la­tor. But it will be use­ful if you­’ll ever need to run an installer or some­thing simi­lar. All the com­mand line opti­ons of PsExec are descri­bed here.

Finally, run the playbook:

ansible-playbook playbook.yml

Notes

  • To auto­ma­te inter­acti­on with Win­dows apps, have a look at Auto­It.
  • You may need to turn off UAC. Sin­ce I dis­cou­rage doing so, I will not pro­vi­de a link on how to do that.

Comments

2 responses to “How to run interactive .exe on Windows with Ansible”

  1. The­re is a win_psexec modu­le which allows to do exact­ly why you­’re doing. It actu­ally dele­ga­tes a com­mand to ano­ther host to run psexec from.

  2. Hmm.. Try­ing your exam­ple and it doesnt seem to be wor­king. It han­gs on run exe task. Here is my playbook


    – name: cre­a­te file
    hosts: windows
    tasks:

    – name: run exe
    win_command: C:\Users\Administrator\intelex\PsExec.exe ‑accep­te­u­la ‑noban­ner ‑i 1 ‑s calc.exe
    regis­ter: output

    The pla­y­book also errors on syn­tax error when run with quo­tes in your example.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.