Starting with version 1.7, the orchestration tool Ansible has a support for managing Windows machines. What the documentation does not provide is a guide on how to run interactive applications properly.
The problem
If you try running any interactive app using the raw or win_command modules, 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=outputthe execution will hang at the Run Windows Calculator step, with no application running. What actually happens is that the application starts in the background in a different session, without displaying the UI to you.
Solution
What you need to do is to get your app running in the session you are observing. If there is only one user logged in, it will be session number 1. There are several hackish, long and complicated ways of doing that. And there is one simple one. You can run the app through a program called PsExec, which will run it in correct session:
- Download the PsTools package that contains the
PsExec.exeprogram. - Extract
PsExec.exeto some folder<WORKING_FOLDER>. - Use the following playbook (replace
<WORKING_FOLDER>with the actual location):
- name: Test PsExec
hosts: windows
tasks:
- name: Copy PsExec
win_copy:
src: <WORKING_FOLDER>/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 option will cause running the app in session 1. The -s option will run the app as system. This is not required for running a Windows Calculator. But it will be useful if you’ll ever need to run an installer or something similar. All the command line options of PsExec are described here.
Finally, run the playbook:
ansible-playbook playbook.yml

Leave a Reply