使用 Ansible 在 Windows 上执行 .exe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27862267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute .exe on Windows with Ansible
提问by Nico
We want to deploy an application on a Windows Server 2012 with Ansible 1.8.2.
我们想要在带有 Ansible 1.8.2 的 Windows Server 2012 上部署应用程序。
I have searched and found a listof modules for Windows. Is there a module to execute a .exe?
我搜索并找到了适用于 Windows 的模块列表。是否有执行 .exe 的模块?
Did someone already launch a .exe on Windows with Ansible?
有人已经使用 Ansible 在 Windows 上启动了 .exe 吗?
回答by vivo
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):
文档说“请注意,还有一些其他不以“win”开头的 Ansible 模块也可以运行,包括“slurp”、“raw”和“setup”(这就是事实收集的工作原理)。(http://docs.ansible.com/intro_windows.html),所以我认为“原始”模块(http://docs.ansible.com/raw_module.html)应该可以工作(我目前没有 Windows VM可以玩):
So please try a playbook with:
因此,请尝试使用以下内容的剧本:
- raw: <your .exe>
or an Ansible adhoc command:
或 Ansible 临时命令:
ansible <your server> -m raw -a '<your .exe>'
回答by sfuqua
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:
raw
正如其他人所建议的那样,该模块可以工作。一个挑战是它不会“知道”可执行文件之前是否已经运行过。结合win_stat
模块和when
条件,您可以构建一个脚本来检测是否已安装某些内容并在未安装时运行。例如,我想安装MSBuild 开发工具:
- name: Check to see if MSBuild is installed
win_stat: path='C:\Program Files (x86)\MSBuild.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 的命令行参数
.\BuildTools_Full.exe /h
回答by jonashackt
There′s another way (and modules) which is not so obvious in the first place: the win_service modulecombined with the win_nssm module.
还有另外一种方式(和模块),这是不是摆在首位那么明显:该win_service模块与合并win_nssm模块。
As sfuquaalready 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 serviceis a very good solution. And it′s very easy to get such a service through the usage of the Non-Sucking Service Manager (nssm).
正如sfuqua已经提到的,大多数时候你想知道你的应用程序的“状态”——例如它是否已经安装、当前正在运行、停止等。因此,Windows 服务的概念是一个非常好的解决方案。并且通过使用Non-Sucking Service Manager (nssm)很容易获得这样的服务。
With the Ansible win_nssm modulethat′s a cakewalk:
使用 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
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:
现在我们有了一个真正的 Windows 服务,可以在win_service 模块的帮助下操作状态,就像我们习惯于在 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.
这种方法使我们无需使用原始模块(它有一些缺点,例如禁用更改处理程序支持)以及为这个简单任务编写和维护脚本的麻烦。
回答by rlat
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:
正如这里提到的,您可以使用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
回答by Anuradha Fernando
I have resolved the issue with psexec
我已经用 psexec 解决了这个问题
In the Playbook
在剧本中
- name: test raw module
hosts: Windows
gather_facts: false
tasks:
- name: Stop process 01
script: startProcess.ps1
And 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
You need to install the psexec in the remote PC. Switches for the psexec
您需要在远程 PC 上安装 psexec。psexec 的开关