在Windows上使用Ansible执行 .exe 文件

8
我们希望使用Ansible 1.8.2在Windows Server 2012上部署应用程序。
我已经搜索并找到了一个针对Windows的模块列表(链接)。是否有一个可以执行.exe文件的模块?
是否已有人在Windows上使用Ansible运行过.exe文件?
5个回答

8
< p > 如其他人所建议的那样,< code > raw 模块可以工作。 其中一个挑战是它不会“知道”可执行文件是否已经运行过。 结合 < code > win_stat 模块和 < code > when 条件,您可以构建一个脚本,检测某些东西是否已经安装并在未安装时运行。 例如,我想安装 MSBuild开发工具

- 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

请注意,我通过手动运行找到了BuildTools_Full.exe的命令行参数。
.\BuildTools_Full.exe /h

7
文件说明中提到:“请注意,还有一些不以“win”开头的其他Ansible模块也可以使用,包括“slurp”、“raw”和“setup”(这是事实收集的方式)。’(http://docs.ansible.com/intro_windows.html),因此我认为‘raw’模块(http://docs.ansible.com/raw_module.html)应该可以使用(我当前没有可用的Windows虚拟机来尝试):"
请尝试使用以下的playbook:
- raw: <your .exe>

或者使用 Ansible 的临时命令:
 ansible <your server> -m raw -a '<your .exe>'

1
刚试了一下,“raw”在Windows Server 2012 R2上运行可执行文件非常好。 - sfuqua

4
此处所述,您可以使用win_command。但如果您需要运行一个交互式的.exe文件,则可能需要通过PsExec来运行它。然后,示例Playbook可能如下所示:
 - 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

4
有另一种(和模块)方法,起初并不那么明显:win_service模块win_nssm模块相结合。
正如sfuqua已经提到的,大多数时候您想要知道应用程序的“状态”——例如是否已安装、当前正在运行、停止等。因此,Windows服务的概念是一个非常好的解决方案。而且,通过使用Non-Sucking Service Manager (nssm)很容易获得这样的服务。
使用Ansible win_nssm模块就像小菜一碟:
  - name: Install & start application as Windows service (via nssm)
    win_nssm:
      name: "your_app_name"
      application: "{{path_to_your_apps_exe}}"
      state: restarted

现在我们有一个真正的Windows服务,并且可以借助win_service模块来操作状态,就像我们习惯于在Linux上运行的应用程序一样:
  - name: Control app Windows service
    win_service:
      name: "your_app_name"
      state: stopped

这种方法使我们不需要使用原始模块(该模块存在一些缺点,例如禁用更改处理程序支持),也不需要为这个简单的任务编写和维护脚本。

1

我已经解决了与psexec有关的问题。

在Playbook中。

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

而 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

您需要在远程计算机上安装psexec。psexec的开关


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接