Thomas Deranek
Thomas Deranek

Reputation: 353

How can a cookbook assign a Chef attribute based on whether a package is installed?

I have a specific Chef attribute which I want to vary based on whether a package is installed. For example:

if package X is installed
  default['attr1']['attr2'] = 'value1'
else
  default['attr1']['attr2'] = 'value2'
end

This attribute is then later used to set certain values in template files that are laid down.

After looking online for the best way to do this, I am finding that it is definitely not common practice. This makes me wonder if I am taking the wrong approach to my problem as well.

So my full question here would be, what is the best way to determine whether a package is installed in a Chef attributes file. Is it not recommended that you vary an attribute based on the current config of the system? If not, then why?

Upvotes: 1

Views: 36

Answers (1)

coderanger
coderanger

Reputation: 54211

Ohai already does this for you at the start of the run via node['packages']. In general Chef code should be describing the desired state of the system, not reacting to the current state. Way down in the depths of Chef's own code it has to do a test-and-repair loop, but your code generally shouldn't need to do that. See https://coderanger.net/thinking/ for more details.

Upvotes: 2

Related Questions