Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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,['my_attribute'])


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

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

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

The ideal way to use attributes is to write them into your cookbook or into a data bag. Here are the file locations for either:

Chef Repo Directory Layout

|- chef-repo

|---- cookbooks

|------- attributes

|---------- default.rb

|------- recipes

|---------- default.rb

|---- data_bags

|------- users

|---------- john.json

|---------- susan.json

|------- databases

|----------- master.json

|----------- slave.json

You can store data in your data bag files in basic JSON format ( knife create data_bag users).

Create user data bag for john:

$ knife data bag create users john

{ "id" : "john" "age" : "27" "height" : "60in" }

Using Data Bags

If you choose to use data bags (I recommend them if you have a lot of different users or database servers (in this context)), you can view more information at Additionally, it's easier to create a users directory in your data_bags directory, and create the user .json files in there (~/chef-repo/data_bags/users/john.json) with the content above, then upload the data bag to the chef server: knife data bag from file users /path/to/data_bags/users/john.json

Using Attributes

You can also store the data in your attributes file:

attributes file

$ vi ~/chef-repo/cookbooks/my-cookbook-name/attributes/default.rb

default['my-cookbook-name']['user-1'] = "John"

default['my-cookbook-name']['user-1']['age'] = "27"

default['my-cookbook-name']['user-1']['height'] = "60in"

cookbook recipe

$ vi ~/chef-repo/cookbooks/my-cookbook-name/default.rb

template "/root/user-list" do action :create source "user-list.erb" end


$ vi ~/chef-repo/cookbooks/my-cookbook-name/templates/default/user-list.erb



<title>My User List</title>



<h1>User List</h1>

User 1: <%= node['my-cookbook-name']['user-1'] %>

User Age: <%= node['my-cookbook-name']['user-1']['age'] %>

User Height: <%= node['my-cookbook-name']['user-1']['height'] %>



share|improve this answer

Your Answer


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.