1

I have a windows_package resource that installs jdk 8 on a windows machine and it looks like this:

java_source="C:\\chef_solo\\utils\\jdk-8u121-windows-x64.exe"
package_name="Java(TM) SE Development Kit 8 (64-bit)"
windows_package package_name do
  source java_source
  action :install
  installer_type :custom
  options "/s #{additional_options}"
end

The problem is that every chef run, it install the same jdk and after the 2nd installation, java is not working anymore.

I was looking for windows_package guards, but couldn't find any. That's because they do not exists?

Anyway, I've found some powershell solutions that check if java exists and I need to know if I can use them somehow as a guard for my windows_package installation.

The solutions are here: How to get the Java version in PowerShell

gbaii
  • 433
  • 1
  • 6
  • 14

2 Answers2

2

Guard clauses are a core feature available on all resources, so there is nothing specific to windows_package to find. You can find detailed info on not_if, only_if, and guard_interpreter in the Chef docs.

coderanger
  • 52,400
  • 4
  • 52
  • 75
1

So, following @coderanger 's idea, The final result is this one:

windows_package package_name do
  source java_source
  action :install
  installer_type :custom
  options "/s #{additional_options}"
  guard_interpreter :powershell_script
  only_if "!(dir 'HKLM:\\SOFTWARE\\JavaSoft\\Java Runtime Environment'  | select -expa pschildname -Last 1)"
end

If you have a better solution, please let me know.

Thank you.

gbaii
  • 433
  • 1
  • 6
  • 14