variable scope is making my head hurt

6 views
Skip to first unread message

Ben Beuchler

unread,
Jan 15, 2009, 6:11:39 PM1/15/09
to puppet...@googlegroups.com
I'm trying to add a "node type" variable that can be interpolated into
source values. In this, my latest attempt, $nodetype always ends up
as "base":

# manifests/nodes
$nodetype = "base"

node "base" {
include postfix
...snip...
}

node "www01", "www02", ... , "www10" inherits "base" {
$nodetype = "wwwnode"
include postfix::custom
}

# modules/postfix/manifests/init.pp
class postfix {
package {"postfix": ensure => installed}
file {"/etc/postfix/main.cf":
content => template("puppet:///files/main.cf.erb})
}
}

# modules/postfix/manifests/custom.pp
class postfix::custom inherits postfix {
File ["/etc/postfix/main.cf"] {
content => undef,
source => ["puppet:///files/$hostname/main.cf",
"puppet:///files/$nodetype/main.cf" ]
}
}


This seems almost exactly the same as the "variable scope" example
from the language tutorial, but there's clearly a subtltly that I'm
missing. Can anyone point it out?

And is there a better way of accomplishing this? I'm trying to avoid
needing to create a unique class for every single kind of server while
at the same time trying to avoid a separate (identical) config file
for every server within a "kind".

Thanks...

-Ben

Richard

unread,
Jan 16, 2009, 2:24:16 PM1/16/09
to Puppet Users
OK, this one I know! I had the same problem and while my solution is
not "pretty" it does seem to work. :) It's just three easy steps
(and you've already done the first one!)

1) Define your "global" variable in a node definition (i.e. $nodetype)

2) Create a mapping class to map those "global" variables to a class
level variable
class global {
$NODE_TYPE = $nodetype
}

3) include the global class in your node.
node "www01", "www02", ... , "www10" inherits "base" {
$nodetype = "wwwnode"
include global
include postfix::custom
}

Now you should be able to reference the $NODE_TYPE variable in any
class that is included after the global class. Hey, I said it was
ugly...

Later...
Richard
Reply all
Reply to author
Forward
0 new messages