Friday, 27 April 2012

A "Hello, World" with Attributes from Chef

Following on from the , this post continues to explore Chef from a development perspective. parameterise scripts. Let's introduce an attribute...


Begin by creating a new exercise, to run the with_attributes recipe (created later) from our hello_world . Save
{"run_list": [ "recipe[hello_world::with_attributes]" ]}
into config/from-hello_world-run-with_attributes.json. When this exercise is run

$ sudo chef-solo --config config/chef-solo-config.rb 
    --json-attributes config/from-hello_world-run-with_attributes.json 
[...] INFO: Starting Chef Solo Run
[...] INFO: Replacing the run_list with 
   ["recipe[hello_world::with_attributes]"] from JSON
/usr/lib/ruby/1.8/chef/cookbook.rb:166:in `load_recipe': 
 Cannot find a recipe matching with_attributes in cookbook hello_world (ArgumentError)
  from /usr/lib/ruby/1.8/chef/mixin/language_include_recipe.rb:38:in `include_recipe'
  from /usr/lib/ruby/1.8/chef/mixin/language_include_recipe.rb:26:in `each'
  from /usr/lib/ruby/1.8/chef/mixin/language_include_recipe.rb:26:in `include_recipe'
  from /usr/lib/ruby/1.8/chef/compile.rb:145:in `load_recipes'
  from /usr/lib/ruby/1.8/chef/compile.rb:144:in `each'
  from /usr/lib/ruby/1.8/chef/compile.rb:144:in `load_recipes'
  from /usr/lib/ruby/1.8/chef/compile.rb:54:in `initialize'
  from /usr/lib/ruby/1.8/chef/client.rb:329:in `new'
  from /usr/lib/ruby/1.8/chef/client.rb:329:in `converge'
  from /usr/lib/ruby/1.8/chef/client.rb:108:in `run_solo'
  from /usr/lib/ruby/1.8/chef/application/solo.rb:198:in `run_application'
  from /usr/lib/ruby/1.8/chef/application/solo.rb:190:in `loop'
  from /usr/lib/ruby/1.8/chef/application/solo.rb:190:in `run_application'
  from /usr/lib/ruby/1.8/chef/application.rb:62:in `run'
  from /usr/bin/chef-solo:24
Chef informs us that the recipe is not found. Creating the missing recipe
$ touch cookbooks/hello_world/recipes/with_attributes.rb
allows a run

$ sudo chef-solo --config config/chef-solo-config.rb 
  --json-attributes config/from-hello_world-run-with_attributes.json 
[...] INFO: Starting Chef Solo Run
[...] INFO: Replacing the run_list 
  with ["recipe[hello_world::with_attributes]"] from JSON
[...] INFO: Chef Run complete in 0.855315 seconds
which successfully does nothing.
Attributes are mashed into the node. Saving
log("Hello, #{node['subject']}") { level :info }
as our with_attributes recipe (cookbooks/hello_world/recipes/with_attributes.rb) looks up the value of the subject attribute, and fills it in. Running Chef now
$ sudo chef-solo --config config/chef-solo-config.rb 
   --json-attributes config/from-hello_world-run-with_attributes.json 
[...] INFO: Starting Chef Solo Run
[...] INFO: Replacing the run_list with 
  ["recipe[hello_world::with_attributes]"] from JSON
[...] INFO: Hello, 
[...] INFO: Chef Run complete in 0.847411 seconds
shows that unset attributes are accessible. Alter the recipe to inspect the attribute value

log("Hello, #{node['subject'].inspect}") { level :info }
and running Chef shows the value is nil
$ sudo chef-solo --config config/chef-solo-config.rb 
  --json-attributes config/from-hello_world-run-with_attributes.json 
[...] INFO: Starting Chef Solo Run
[...] INFO: Replacing the run_list with 
  ["recipe[hello_world::with_attributes]"] from JSON
[...] INFO: Hello, nil
[...] INFO: Chef Run complete in 0.854488 seconds
Altering config/from-hello_world-run-with_attributes.json to

{"run_list": [ "recipe[hello_world::with_attributes]" ], "subject":"HAL"}
shows how Chef allows access to the environmental context through attributes

$ sudo chef-solo --config config/chef-solo-config.rb 
   --json-attributes config/from-hello_world-run-with_attributes.json
[...] INFO: Starting Chef Solo Run
[...] INFO: Replacing the run_list with 
  ["recipe[hello_world::with_attributes]"] from JSON
[...] INFO: Hello, HAL
[...] INFO: Chef Run complete in 0.90974 seconds
which may be override, for example within the script by
node["subject"] = "Dave"
log("Hello, #{node['subject']}") { level :info }
Running this overrides HAL with Dave
$ sudo chef-solo --config config/chef-solo-config.rb 
  --json-attributes config/from-hello_world-run-with_attributes.json 
[...] INFO: Starting Chef Solo Run
[...] INFO: Replacing the run_list 
  with ["recipe[hello_world::with_attributes]"] from JSON
[...] INFO: Hello, Dave
[...] INFO: Chef Run complete in 0.882525 seconds
And that attributes's done in time for tea.
One or two more sophisticated examples are queued in the draft pipeline, so check back later for more on Chef - or subscribe to the feed.

No comments:

Post a Comment