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


Chronological Thread 
  • From: "Mark J. Reed" < >
  • To:
  • Subject: [chef] Re: Re: Re: Re: Re: Re: How do I test an attribute value in an
  • Date: Mon, 27 Jun 2011 09:12:28 -0400
  • Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=qf3wkIvmJvY6Hp9MHqGJ40v8LI0ZlbbJw0Z47Lxy/0hXcNBXKRT9+giFgm9QMDH4bf rFNYDwegyLkdlEr3vlHaZZ6q6I5JcM4oSJS9PTtTPe7JX3ofs7NEdKnblOmj1c8nvuUM kDHmFmbBgcEnS5sfdpFVE9jsZLl7EvX995skU=

On Mon, Jun 27, 2011 at 4: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
[...]
How do I reference the attribute inside a recipe?

Yes, there are lots of ways to *set* attributes, that override each other depending on the precedence rules on the wiki.  But the end result is that all of those attributes end up as fields on the "node" object.  No matter how the attribute gets set, you access it via node. 

Cookbook:

default[:my_cookbook][:my_attribute] = "default"

Role:

override[:my_cookbook][:my_attribute] = "overridden"

Checking in recipe:

case node[:my_cookbook][:my_attribute] 
  when "default" then puts "Still default!"
  when "overridden" then puts "Here there be roles!"
end

One thing to note is that the node object is not a normal Ruby Hash, but rather special object that has some magic around accessing its fields.  Specifically, these three expressions all return the same value:

node[:my_cookbook]
node['my_cookbook']
node.my_cookbook

The recommended notation is the middle one, since symbols can be offputting to the uninitiated and method calls run the risk of conflicting with actual methods on the object. 

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?

The above code is checking whether or not any attribute file or role or recipe or anything else has set [:chef][:webui_enabled] on the node.  node.chef (same as node['chef'] or node[:chef]) returns the entire subhash of attributes defined as [:chef][:something, and the .attribute? call returns true if the hash it's called on contains the named attribute.  The method returns true if the attribute has a value,  even if that value itself is false, which is the reason you would use it instead of just "if node.chef.webui_enabled" or similar.


Is it:
if node.apache.dir?
or
if node.chef.attribute?("apache""dir")
or
something else completely?

If you set the attribute with something[:apache][:dir], then the equivalent of the above example would be this:


if node.apache.attribute?("dir") then
...
end

 -- 
Mark J. Reed < "> >




Archive powered by MHonArc 2.6.16.

§