Execute .exe on Windows with Ansible

2019-03-20 10:04发布

问题:

We want to deploy an application on a Windows Server 2012 with Ansible 1.8.2.

I have searched and found a list of modules for Windows. Is there a module to execute a .exe?

Did someone already launch a .exe on Windows with Ansible?

回答1:

The documentation says 'Note there are a few other Ansible modules that don’t start with “win” that also function, including “slurp”, “raw”, and “setup” (which is how fact gathering works).' (http://docs.ansible.com/intro_windows.html), so I would assume that the 'raw' module (http://docs.ansible.com/raw_module.html) should work (I have no Windows VM currently available to play around):

So please try a playbook with:

- raw: <your .exe>

or an Ansible adhoc command:

 ansible <your server> -m raw -a '<your .exe>'


回答2:

The raw module can work, as others have suggested. One challenge is that it won't "know" if the executable has already been run before. In combination with the win_stat module and the when conditional, you can build a script that detects if something has been installed and runs if not installed. For example, I wanted to install the MSBuild development tools:

- name: Check to see if MSBuild is installed
  win_stat: path='C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe'
  register: msbuild_installed
- name: Download MS Build Tools 2013
  win_get_url:
    url: 'http://download.microsoft.com/download/9/B/B/9BB1309E-1A8F-4A47-72A3B3/BuildTools_Full.exe'
    dest: 'c:\temp\BuildTools_Full.exe'
  when: not msbuild_installed.stat.exists
- name: Install MS Build Tools 2013
  raw: 'c:\temp\BuildTools_Full.exe /Quiet /NoRestart /Full'
  when: not msbuild_installed.stat.exists

Note that I found the command line arguments for BuildTools_Full.exe by manually running

.\BuildTools_Full.exe /h


回答3:

There´s another way (and modules) which is not so obvious in the first place: the win_service module combined with the win_nssm module.

As sfuqua already mentioned, most of the time you want to know the "state" of your application - e.g. if it was already installed, is currently running, stopped and so on. Therefore the concept of a Windows service is a very good solution. And it´s very easy to get such a service through the usage of the Non-Sucking Service Manager (nssm).

With the Ansible win_nssm module that´s a cakewalk:

  - name: Install & start application as Windows service (via nssm)
    win_nssm:
      name: "your_app_name"
      application: "{{path_to_your_apps_exe}}"
      state: restarted

Now we have a real Windows service and can manipulate the state with the help of the win_service module, just as we are used to from applications running on Linux:

  - name: Control app Windows service
    win_service:
      name: "your_app_name"
      state: stopped

This approach frees us of the need to use the raw module (which has some disadvantages, like disabling change handler support) and the troubles to write and maintain scripts for this simple task.



回答4:

As mentioned here, you can use win_command. But if you need to run an interactive .exe, you may need to run it through PsExec. An example Playbook can then look like this:

 - 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


回答5:

I have resolved the issue with psexec

In the Playbook

- name: test raw module
  hosts: Windows
  gather_facts: false
  tasks:
    - name: Stop process 01
      script: startProcess.ps1

And startProcess.ps1

#Creating the credential for the invoke-command.
$strScriptUser = "COMPUTERNAME\USer"
$strPass = "PASSWORD"
$PSS = ConvertTo-SecureString $strPass -AsPlainText -Force
$cred = new-object system.management.automation.PSCredential $strScriptUser,$PSS

#Invoke-Command to call the psexec to start the application.

invoke-command -Computer "." -Scriptblock {
c:\AnsibleTest\ps\psexec.exe -accepteula  -d -h -i 1 -u COMPUTERNAME\USER -p PASSWORD PATH_TO_THE_EXE\PROGRAM.EXE
}  -Credential $cred

You need to install the psexec in the remote PC. Switches for the psexec