Dynamically set Chef resource attributes

When you need to set a Chef resource attribute based on the current state of the environment, there is a way to dynamically provide the value.

  1. Set the value of a boolean variable with a test,
  2. Declare the Chef resource and assign a reference to it to a variable,
  3. Set the resource attribute based on the value of the boolean variable.
# Set a boolean variable with a test 
def MY_BOOLEAN_VARIABLE?
  "#{node['domain']}" != ""
end 

# Execute a resource and get a reference to it into a variable
t = MY_RESOURCE 'MY_RESOURCE_NAME' do
  ...
end
# Set the attribute value based on the boolean variable
t.MY_RESOURCE_ATTRIBUTE MY_ATTRIBUTE_VALUE if MY_BOOLEAN_VARIABLE?

 

 

Leave a Reply