用Ansible在Windows上执行.exe

我们想用Ansible 1.8.2在Windows Server 2012上部署应用程序。

我已经search并find了 Windows的模块列表 。 有没有执行.exe的模块?

Ansible有没有人在Windows上启动过.exe?

文档说'注意还有一些其他Ansible模块不以“win”开头,也包括“slurp”,“raw”和“setup”(这是事实收集的工作原理)。 ( http://docs.ansible.com/intro_windows.html ),所以我会假设“原始”模块( http://docs.ansible.com/raw_module.html )应该工作(我目前没有Windows VM可供玩耍):

所以请尝试一下剧本:

- raw: <your .exe> 

或者一个Ansible adhoc命令:

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

raw模块可以像其他人所建议的那样工作。 一个挑战是它不会“知道”可执行文件是否已经运行过。 结合win_stat模块和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 

还有另一种方式(和模块),首先不太明显: 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 

这种方法使我们无需使用原始模块(这有一些缺点,如禁用更改处理程序支持)以及编写和维护这个简单任务的脚本的麻烦。

如上所述,您可以使用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 

我已经用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 

您需要在远程PC中安装psexec。 切换为psexec