Only_if a package is installed


#1

Is there a quick way to check if node attribute present?

I know the packages attribute has bash as a installed package, so how can I check for this

only_if “node[‘packages’].attribute?(‘bash’)”

what I ever I try in only_if it says skipped due to only_if. What is proper syntax?

Thanks in advance

M


#2

I know the packages attribute has bash as a installed package, so how can I check for this

only_if "node[‘packages’].attribute?('bash')"

what I ever I try in only_if it says skipped due to only_if. What is proper syntax?

Can you share more details about the resource are you trying to run only if bash is installed? There may be another way to accomplish your goal.

But to answer your current specific question, you can definitely test for the presence of an attribute.

The node['packages'] attribute comes from data about the host and is populated by ohai. node['packages'] is a Hash of keys and values. Keys are the names of the packages installed, the values are details like version and arch.

To ask if bash is installed already, the following would return true or false:

node['packages'].keys.include? "bash"
=> true

So if you want the resource containing this only_if to perform its action only when the bash package is installed:

only_if { node['packages'].keys.include? "bash" }


#3

Is there any reason not to simply check if the file “/bin/bash” exists? Yes, checking the list of packages can be useful, but why wouldn’t you sumply check fo the presence of a file, which requires much less complexity to verify?


#4

node[‘packages’] will be generated at the beginning of the chef run, which could by out of date by the time the only_if statement executes, unless you are diligent to reload ohai packages.

It would probably be more reliable to simply test for the existence of the binary.

only_if "type /bin/bash"

#5

Thanks robbkidd,

That helps. All I wanted to do was if a package is installed do something. I saw chef documentation and saw attribute? and member? but didn’t see keys.include? where will I get information of all the methods that can be applied on node attribute?

I understand I have to run upload ohai prior to this to refresh the list updated by recipes etc.

I am sure I can also run a command in only_if to check presence of a package…may be that is better way of doing this.

Thanks for you your help.

M


#6

nkadel and spuder raise good questions.

What is it that you want to do if bash is present on the host?