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.
- Set the value of a boolean variable with a test,
- Declare the Chef resource and assign a reference to it to a variable,
- 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?