Reputation: 1186
What would be most polite way to have a default value for an attribute in chef
that is used in template?
so in default/attributes.rb
I have a default value:
default['my_attribute'] = 'some_default_value'
I'd like to override it on some chosen nodes with node attributes:
node['my_attribute'] = 'some_defined_value'
And in my templates I'd like to just use:
@my_attribute
which would take value from default['my_attribute'] or node['my_attribute'] (if defined)
What would be the most polite and Chef
'ish way to do that?
Upvotes: 0
Views: 1627
Reputation: 26997
Ohai!
First, I would recommend you have a look at the attribute-precedence model for reference.
You are correct, in your attribute file, define something like this (note, it's common practice to namespace attributes after the cookbook):
default['cookbook']['attribute'] = 'original_value'
And then in a recipe, you can mutate the node
object by calling node.set
:
node.set['cookbook']['attribute'] = 'new_value'
This will save the current node state and persist back to the server (if using a Chef Server).
Then, in your template
resource, just pass the value in directly:
template '/tmp/foo.txt' do
variables(
:my_attribute => node['cookbook']['attribute']
)
end
Upvotes: 2