Run chef block only if chocolatey is installing package

285 views Asked by At

I'm having an issue with determining if a package was installed or was already installed using chef's chocolatey_package functionality. Pretty much, I'm installing a package that requires an iisreset afterwards. I only want iis to be reset when the package is installed and NOT on subsequent executions of chef-client (where chocolatey will exit early because the package was already installed).

I am storing what packages are being installed in an attributes file. It uses the following format:

{'name'=>'blah', 'version'=>'1.0.0'[, 'should_notify'=>'reset iis']}

Here's what I have so far, code-wise. How can I modify this to branch based on what action chocolatey performs (install or ?

include_recipe 'chocolatey::default'

node['cookbook-name']['choco_packages'].each{ |package|
  chocolatey package['name'] do
  version package['version']
  action :install

  if package.instance_variable_defined?(:@should_notify) && package.should_notify == 'reset iis'
    notify :run, 'execute[reset iis]', :immediately
    end
  end
}

execute 'reset iis' do            
  command 'iisreset'
  action :nothing
end

Thanks

1

There are 1 answers

1
Szymon On BEST ANSWER

Your attributes snippet isn't correct, For simplicity, I'll assume it should be:

[{ 'name' => 'blah', 'version' => '1.0.0', 'should_notify' => 'restart_iis' }]

Chocolatey shouldn't install the package if it's installed already. So this should work fine:

node['cookbook-name']['choco_packages'].each do |package|
  chocolatey package['name'] do
    version package['version']
    notifies :run, "execute[#{package['should_notify']}]"
  end
end

execute 'reset iis' do
  command 'iisreset'
  action :nothing
end

Without :immediately it will restart IIS ONCE at the end of chef run, regardless of number of packages. If you'll add :immediately, your IIS will be restarted after each package install.

It won't restart IIS if package is already installed.