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.
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.
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.