Puppet: variable overriding best practices

puppet

I'm wondering what are best practices for overriding variables in puppet. I'd like to have all my nodes (which located in different places, some of them are qa, some live) with same classes. Now I have something like this:

class base_linux {
   <...> # something which requires $env and $relayhost variables
}

class linux_live {
   $relayhost="1.1.1.1"
   $env = "prod"

   include base_linux
}

class linux_qa {
   $relayhost="2.2.2.2" # override relayhost

   include base_linux
}

class linux_trunk {
   $env = "trunk" # override env

   inlude linux_qa
}

node "trunk01" {
   include linux_trunk
   include <something else>
}
node "trunk02" {
   $relayhost = "3.3.3.3" # override relayhost
   include linux_trunk
   include <something else>
}

node "prod01" {
   include linux_prod
}

So, I'd like to have some defaults in base_linux, which can be overrided by linux_qa/linux_live, which can be overrided in higher level classes and node definition.

Of course, it does not work (and not expected to work). It does not work with class inheritance as well. Probably, I'll be able to archive using global scope variables, but it does not seems like a good idea to me.

What is the best way to solve this problem?

Best Answer

The best practice depends on the number of nodes/systems you manage. Modeling data in puppet manifests is sufficient if you can keep track of the data (I've seen it done with thousands of systems, however it gets challenging if you have a large number of variables and deep inheritance especially if you start referencing by scope such as $value = $some_class::some_var). If you have a complex infrastructure it makes sense to look at separating the data from puppet manifests using extlookup, heira, or an external node classifier (ENC).

The way you have done it in the puppet manifests is using dynamic scoping and node inheritance, and dynamic scoping is being deprecated in Puppet 2.7. If this is rewritten in extlookup, it would simply be:

# configured globally
# linux_qa is more likely linux_%{env}
$extlookup_precedence = ["%{fqdn}", "linux_trunk", "linux_qa", "common"]

# in the appropriate class
$relayhost = extlookup("relayhost")