# How to override attribute values at chef-client runtime

**URL:** <https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808>\
**Category:** Chef Infra (archive)\
**Created:** [October 24, 2017, 2:46pm UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808 "2017-10-24T14:46:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![priyanshu](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@priyanshu](https://discourse.chef.io/u/priyanshu)\
**Post date:** [October 24, 2017, 2:46pm UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/1 "2017-10-24T14:46:50Z")

</div>

Suppose I have an attribute file having some attributes say  
default[‘xyz’][‘abc’]=‘value1’

now when i run chef-client , i want to override this attribute value at chef-client run to some other value such that , that passed value is used in a recipe wherever needed.

All in al, how can we override the default attribute value to some other value at runtime.

---

<div class="post-metadata">

**Author:** ![joerg.herzinger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joerg.herzinger/32/1514_2.png) [@joerg.herzinger](https://discourse.chef.io/u/joerg.herzinger)\
**Post date:** [October 24, 2017, 3:03pm UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/2 "2017-10-24T15:03:09Z")

</div>

If by “at runtime” you mean really for this single chef-client run, then there is the “-j” option of the chef-client which lets you load a JSON file with attributes. But the recommended approach usually is setting such attributes at a node, role, environment level or in a DataBag/Vault.

---

<div class="post-metadata">

**Author:** ![priyanshu](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@priyanshu](https://discourse.chef.io/u/priyanshu)\
**Post date:** [October 25, 2017, 7:03am UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/3 "2017-10-25T07:03:07Z")

</div>

Can you please show an example so that i can understand how can i override the attribute value at chef-client run???  
,my attribute is  
default[‘cookbookname’][‘value1’]=‘tst1234’

If I run my chef client as follows  
chef-client --runlist ‘recipe[cookbookname::recipename]’

I want that when i run chef client as mentioned above but the value of attribute should be passed by me at runtime and tst1234 should be overriden

---

<div class="post-metadata">

**Author:** ![simark](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/simark/32/2099_2.png) [@simark](https://discourse.chef.io/u/simark)\
**Post date:** [October 25, 2017, 7:19am UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/4 "2017-10-25T07:19:54Z")

</div>

Like @joerg.herzinger said there are many approaches on how you can achieve this for example passing a json to the chef-client.  
First create json file with the following content:

```json
{
  "cookbookname": {
    "value1": "desired_value"
  }
}

```

Then pass it to the chef-client just like @joerg.herzinger said in the previous comment.

---

<div class="post-metadata">

**Author:** ![priyanshu](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@priyanshu](https://discourse.chef.io/u/priyanshu)\
**Post date:** [October 25, 2017, 7:24am UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/5 "2017-10-25T07:24:21Z")

</div>

where will i create this json file?? On workstation or on the node??

Because i need to run this chef-client on the node?

Please elaborate more

---

<div class="post-metadata">

**Author:** ![joerg.herzinger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joerg.herzinger/32/1514_2.png) [@joerg.herzinger](https://discourse.chef.io/u/joerg.herzinger)\
**Post date:** [October 25, 2017, 8:33am UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/6 "2017-10-25T08:33:01Z")

</div>

Since you need to call `chef-client -j /path/to/attributes.json` on the node, the file obviously also has to be one the node.

---

<div class="post-metadata">

**Author:** ![priyanshu](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@priyanshu](https://discourse.chef.io/u/priyanshu)\
**Post date:** [October 27, 2017, 2:07pm UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/7 "2017-10-27T14:07:33Z")

</div>

How can this be done …if for example we need to perform oracle install on 10 servers at a time and with different attribute values on different nodes.

---

<div class="post-metadata">

**Author:** ![ian](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ian/32/1905_2.png) [@ian](https://discourse.chef.io/u/ian)\
**Post date:** [October 28, 2017, 1:20am UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/8 "2017-10-28T01:20:13Z")

</div>

You need some way to differentiate the nodes (hostname?) and use that with a hash or similar structure. Each node picks a different set of attributes.

Running chef-client against some arbitrary JSON seems like a smell. It’s certainly prone to error. If someone else runs chef-client blindly, or you have chef-client runs happening automagically… it’ll blow away your changes.

---

<div class="post-metadata">

**Author:** ![budgester](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/budgester/32/1004_2.png) [@budgester](https://discourse.chef.io/u/budgester)\
**Post date:** [October 28, 2017, 11:29am UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/9 "2017-10-28T11:29:02Z")

</div>

Are you using a Chef server ? could you override the attributes based on  
the environment and then environment files they are linked to ?

[https://docs.chef.io/environments.html](https://docs.chef.io/environments.html)

---

<div class="post-metadata">

**Author:** ![ian](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/ian/32/1905_2.png) [@ian](https://discourse.chef.io/u/ian)\
**Post date:** [October 28, 2017, 3:38pm UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/10 "2017-10-28T15:38:38Z")

</div>

I override various cookbook attributes, mostly using node.chef\_environment, but sometimes by role too. It’s not always necessary to use the environment file unless you’re straight up overriding existing attributes. You can use conditions to base different values on environment or role(s) at any time. To do slightly more complex things, I use a hash that has values for each environment. This would be in the cookbook attributes.

Something like…

```
default[:myhash] = {
  'prod' => 'foo',
  'staging' => 'bar',
  'test' => 'baz'
}

```

And then call it using…

`node.default[:some_attribute] = default[:myhash][node.chef_environment]`

---

<div class="post-metadata">

**Author:** ![priyanshu](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@priyanshu](https://discourse.chef.io/u/priyanshu)\
**Post date:** [October 30, 2017, 7:26am UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/11 "2017-10-30T07:26:05Z")

</div>

Suppose i want to create a database and i have taken an attribute  
default[‘cookbook’][‘db\_name’]=‘tst1234’

now at chef client run i want to override this value such that db which will be created on node is ‘qst1234’

How will I acheive this?

I have not used environment. Should this attribute value used as environment?

---

<div class="post-metadata">

**Author:** ![priyanshu](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@priyanshu](https://discourse.chef.io/u/priyanshu)\
**Post date:** [November 7, 2017, 7:06am UTC](https://discourse.chef.io/t/how-to-override-attribute-values-at-chef-client-runtime/11808/12 "2017-11-07T07:06:49Z")

</div>

I am running chef-client --runlist ‘recipe[tcookbookname::recipename]’

now i want to override the attribute values. where will i use “-j” option along with the above command?
