I am automating the configuration of some Windows build agents. 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"). 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):
# 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). In the case of git, it works from admin cmd, but not from standard cmd. After reboot, then it works from standard cmd.
Any suggestions? I am relatively new to Ansible so any suggestions would be appreciated.
win_chocolatey now returns a code if a reboot is required and the task registers as changed. I registered my response as install_response and used the following code to conditionally reboot. Command, rc, and stdout are the return values provided by the module.
- 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.
You could use the win_command module and check if the git.exe is where it should be.
Like this:
Test-Path returns "True" if the path exists
-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. 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"
- name: Install git
win_package:
path: C:\temp\git.exe
register: result
- win_reboot:
when: result.reboot_required
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.