[chef] Re: Re: Re: Re: Re: Re: How do I test an attribute value in an


Chronological Thread 
  • From: Tim Diggins < >
  • To:
  • Subject: [chef] Re: Re: Re: Re: Re: Re: How do I test an attribute value in an
  • Date: Mon, 27 Jun 2011 09:52:15 +0100

You can access node attributes in recipes using
node["apache"] # preferred
node[:apache]  # as a symbol, deprecated
node.apache # as a method, deprecated

which works for sub-keys too:
node["apache"]["dir"]
node[:apache][:dir]
node.apache.dir

and test if there is such a attribute using 
node.attribute?("apache")
and 
node.apache.attribute?("dir") 

QUESTION: I suppose that one is a bit deprecated (despite being from the wiki) - should really be: 
node["apache"].attribute?("dir")

Ruby note:
In ruby predicate methods (which test whether something is true or not) often (by convention) end in '?' (like File.exists?() for example))

You might want to check out:
and



Tim


On Mon, Jun 27, 2011 at 9:29 AM, ItsMikeE < "> > wrote:
Thanks for the response
That helps, but still leaves me with some questions

Looking at the wiki page on setting attributes

Default in ../cookbooks/../attributes/default.rb
Example Precedence #1
default["apache"]["dir"] = "/etc/apache2"

Override in .../roles/rolename.rb
Example Precedence #8
override_attributes({ "apache" => {"dir" => "/etc/apache2"}})

How do I reference the attribute inside a recipe?
I don't mind if it is in a case statement, and if statement or an only_if. I am
just trying to choose between one of two source files to overwrite one
destination file.

Again going back to the wiki I can see
if node.chef.attribute?("webui_enabled")
 # set up webui stuff
end

How does that equate to the attributes that were set above?
Is it:
if node.apache.dir?
or
if node.chef.attribute?("apache""dir")
or
something else completely?


I don't really mind if it is in an only_if, or a case statement or an if test.






Archive powered by MHonArc 2.6.16.

§