Reputation: 1
I'm looking to override a role's attributes based on the environment much the same was as I could use env_run_lists to override a role's run_list based on the environment.
In my case it's for Nagios checks. I need a default check, but I also want different types of machine (using roles) to have different levels of check, PLUS I want to have different levels of check for different environments based on the role.
Ideally, I'd want something like this in the role for a machine type (using the hypothetical directive of "env_attributes" in a web server role:
name "webserver"
description "A machine that serves webs"
env_attributes (
"_default" => [
"nagios" => [
"check_interval" => "60"
"retry_interval" => "3
]
],
"CompanyStackA" => [
"nagios" => [
"check_interval" => "10"
"retry_interval" => "2
]
],
"CompanyStackB" => [
"nagios" => [
"check_interval" => "50"
"retry_interval" => "1
]
]
)
What I don't want to have to do is write attributes for every machine type, having defaults is essential, so I'd rather not have a load of attributes set for all roles in actual environment. What would be the simplest way? It seems not being able to override attributes by role+environment seems an oversight if you can for run lists.
Upvotes: 0
Views: 1565
Reputation: 36
Opscode's suggested method for achieving this to do it in individual cookbooks. Their reasoning is that it keeps all your logic contained within the cookbook itself, rather than spread across roles, cookbooks, and environments.
You might be able to create a cookbook specifically to override the attributes you're trying to set. Using this model, the recipe's default.rb might include something along the lines of:
if node.run_list.roles.include?('webserver')
if node.environment == 'CompanyStackA'
node.override['nagios']['check_interval'] = "10"
node.override['nagios']['retry_interval'] = "2"
else if node.environment == 'CompanyStackB'
node.override['nagios']['check_interval'] = "50"
node.override['nagios']['retry_interval'] = "1"
else
node.override['nagios']['check_interval'] = "60"
node.override['nagios']['retry_interval'] = "3"
end
end
Here's a link to an Opscode ticket where they rejected a similar idea based on the grounds that it should be contained within the individual Cookbooks: Support for enviroment attributes in roles
Upvotes: 1