简体   繁体   English

win_chocolatey安装后如何重启

[英]how to reboot after win_chocolatey install

I am automating the configuration of some Windows build agents. 我正在自动化一些Windows构建代理的配置。 I want to install packages, but some commands ( git , hg ) are not available on the command-line until I reboot the machine (oddly, they are available for command-line if I do "Run as Administrator"). 我想安装软件包,但是在重新引导计算机之前,某些命令( githg )在命令行上不可用(如果我以“以管理员身份运行”,则它们可用于命令行)。 I would like to reboot only if specific packages are installed. 我只想在安装了特定软件包的情况下重启。

I saw that I can execute the win_reboot module conditionally (example of rebooting after applying windows updates): 我看到我可以有条件地执行win_reboot模块(应用Windows更新后重新启动的示例):

# Apply updates and reboot if necessary
- win_updates:
  register: update_result
- win_reboot:
  when: update_result.reboot_required

However, I want to do it only if a specific package is installed. 但是,仅在安装了特定软件包的情况下,我才想这样做。 Ideally, it would be something like this: 理想情况下,应该是这样的:

- win_chocolatey: git
  register: git_result
- win_reboot:
  when: git_result.reboot_required

However, I didn't see the win_chocolatey returned any values (and it might not know it needs rebooting). 但是,我没有看到win_chocolatey返回任何值(并且它可能不知道需要重新启动)。 In the case of git, it works from admin cmd, but not from standard cmd. 在git的情况下,它可以通过admin cmd运行,但不能从标准cmd运行。 After reboot, then it works from standard cmd. 重新启动后,便可以从标准cmd运行。

Any suggestions? 有什么建议么? I am relatively new to Ansible so any suggestions would be appreciated. 我对Ansible相对较新,因此任何建议将不胜感激。

win_chocolatey now returns a code if a reboot is required and the task registers as changed. 如果需要重新启动并且任务注册为已更改,则win_chocolatey现在将返回代码。 I registered my response as install_response and used the following code to conditionally reboot. 我将响应注册为install_response,并使用以下代码有条件地重新引导。 Command, rc, and stdout are the return values provided by the module. Command,rc和stdout是模块提供的返回值。

- name: Install a package
  win_chocolatey:
    name: "PackageName"
    state: present
  register: install_response

- win_reboot:
  when: install_response.changed == true and install_response.rc == 3010

Unfortunately win_chocolatey has no return values to check against. 不幸的是,win_chocolatey没有返回值可以检查。

You could use the win_command module and check if the git.exe is where it should be. 您可以使用win_command模块并检查git.exe是否在应有的位置。

Like this: 像这样:

Test-Path returns "True" if the path exists 如果路径存在,则Test-Path返回“ True”

-name: Check git install
 win_command: Test-Path C:\git\location\git.exe
 register: git_loc

-name: reboot if git installed
 win_reboot:
 when: git_loc.stdout == "True"

Obviously there is an issue with this in that once git is installed the machine will reboot every time you run the playbook against it. 显然,这是有问题的,因为一旦安装了git,机器就会在您每次针对它运行剧本时重新启动。 You could add some other guardian variable that indicates an initial run. 您可以添加其他一些指示初始运行的监护变量。

 -name: reboot if git installed
  win_reboot:
  when: 
    - git_loc.stdout == "True"
    - inital_run == "yes" 

Please try with win_package, which has a return value "reboot_required" 请尝试使用win_package,它的返回值为“ reboot_required”

- name: Install git 
  win_package:
  path: C:\temp\git.exe
  register: result
- win_reboot:
  when: result.reboot_required

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM