Package install not_if

I’d like to use not_if as part of my package deployment. I’m a bit new to Ruby/Chef and I’m having some trouble getting this to work like I’d like it to. What I’ve ended up doing is creating the not_if in Powershell, this isn’t the most elegant or efficient way to do this since Ohai already knows if the package is installed or not.

What I’d like to do is something like

not_if { node['packages]['vmware tools']['version'] >= "9.0.5.21789"}

So don’t try to install if vmwarre tools is already present, and the version is less than or equal to “9.0.5.21789”.

Thanks

You can reuse Ruby’s version rules:

not_if { Gem::Version.create(node['packages]['vmware tools']['version']) < Gem::Version.create('9.0.5.21789') }
1 Like

This works if the application is installed however if its not in Ohai its nil, how can I tell Chef not_if nil or less than version number?

I tried this

not_if {  Gem::Version.create(node['packages]['vmware tools']['version']) < Gem::Version.create('9.0.5.21789') || Gem::Version.create(node['packages]['vmware tools']['version']).nil?}

Put the test for nil? first in your OR statement. Statements are evaluated lazily, left to right. By the time your code tests for nil? it is already too late.

Kevin Keane
Whom the IT Pros Call
The NetTech
http://www.4nettech.com
Our values: Privacy, Liberty, Justice
See https://www.4nettech.com/corp/the-nettech-values.html

1 Like

Brilliant! Thank you for the assistance, you’ve made this noob happy.