How to override attribute values at chef-client runtime

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.

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.

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

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:

{
  "cookbookname": {
    "value1": "desired_value"
  }
}

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

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

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.

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.

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.

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

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]

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?

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?