Report this

What is the reason for this report?

Override Attributes during Run Time in Chef

Posted on March 2, 2021

I want to change my attribute value dynamically during run time .

Ex. attribute: foo: ‘Hello’ Now I want to change value of key ‘foo’ to ‘World’.

So is there any way to do that? Please help.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

In Chef, you can certainly change attribute values during runtime. You can do this using the node.override['attribute_name'] in your recipe code.

For example, if you want to change ‘foo’ to ‘World’, you could use:

  1. ruby node.override['foo'] = 'World'

After this line of code, whenever you access node['foo'] in the same Chef run, it will return ‘World’ instead of ‘Hello’.

Remember, this will only affect the current Chef run. If you want the changed value to persist, you will need to save the node at the end of the chef run.

Note: Overriding attributes in Chef should be used sparingly, as it can lead to difficult-to-debug situations because of the way Chef attribute precedence levels work.

For more information about Chef attributes, please check out the official Chef documentation.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.