Way to pass parameters at run time in Chef

chef

I wanted to pass some attributes to chef-client at runtime. Is there a way to do the same?
I was looking into chef-client -j option, but to my knowledge it can be used to specify run_list. Can I pass some attributes in it? If yes, how?

Best Answer

Yes, you can use the -j json file option to populate node attributes.

{
  "my_attribute": "I like attributes!"
}

This will make an attribute named my_attribute available in your cookbooks. For example,

Chef::Log.info(node['my_attribute'])

Or,

if node['my_attribute'] =~ /like/
  package "foo-likes-attributes" do
    action :install
  end
end

Setting an initial run_list is the most common use of the json attributes file for Chef Client. If you're using Chef Client + Chef Server, though, you can simply modify the node object on the server either through the webui (Open Source Chef Server) or management console (Opscode Hosted/Private Chef), or through knife node edit if you're using the command-line tool, knife.

Note that using the JSON file is like modifying the node object on the server, the attributes set here "normal" priority like when they are used in a recipe, and these attribute values will be saved to the Node object on the server at the end of a successful run.