Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
Thanx for the reply. Can i somehow use them in my attribute file.?? My exact use case is like - I want to run a service at a specific port for a specific node and i want to provide it at run time ( chef-client -j ). – –  Manak Wadhwa Mar 4 '13 at 11:40
    
You absolutely can set attributes in a cookbook's attributes file. –  jtimberman Mar 5 '13 at 17:55
add comment

Is there a way to prevent attributes passed by -j option to be saved on the server?

We are using it to force deploy of application, but we only want that value to be used for that run, not later runs.

We can run it twice, once with true, then with false, to revert it, but that seems like an overkill.

EDIT: Fixed by resetting the value of the attribute after it is read, so it will only apply to single run

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.