[Puppet Users] Setting Variable in Class or Node and Using in Included Module

256 views
Skip to first unread message

Brian Pitts

unread,
Apr 26, 2010, 9:54:24 PM4/26/10
to puppet...@googlegroups.com
Should the following approach to optionally including site-specific
information in modules work? It's the third pattern documented at
http://serialized.net/2009/07/puppet-module-patterns/

In my case, I have

modules/manifests/ntp/init.pp

import "classes/*"

if ($ntp_servers) {
$servers = $ntp_servers
} else {
$servers = ["0.rhel.pool.ntp.org", "1.rhel.pool.ntp.org",
"2.rhel.pool.ntp.org"]
notice("ntp: ntp_servers not set. Using red hat's by default.")
}

modules/manifests/ntp/classes/ntp.pp

class ntp {
package { "ntp":
ensure => installed;
}
service { "ntpd":
enable => true,
ensure => running,
subscribe => File["/etc/ntp.conf"];
}
# template iterates over values in $servers array
file { "/etc/ntp.conf":
ensure => file,
owner => "root",
group => "root",
mode => "644",
content => template("ntp/ntp.conf.erb");
}
}

And then in different classes [0] I set the ntp servers and include the
module like:

$ntp_servers = ["timeserver1.example.edu", "timeserver2.example.edu"]
include ntp

I often receive the error message "Failed to parse template
ntp/ntp.conf.erb: Could not find value for 'servers' at
/etc/puppet/modules/ntp/manifests/classes/ntp.pp:19 on node
foo.example.edu" or my own notice() from the manifest above during runs.

I tried moving setting $ntp_servers from a class into a node, but that
did not make a difference.

Can anyone point out what I'm doing wrong or why this approach won't
work? I've read quite a bit on variable scope on the mailing list and
wiki, but I could still be missing something. I'm running puppet 0.24.8
on RHEL5.

[0] I have various nodes defined by function (e.g. database server) that
inherit from a base node. One thing the base node does is check a fact
that returns the location of the client. It then includes a module with
the location-specific configuration, such as the ntp server.

--
Brian Pitts
Systems Administrator | EuPathDB Bioinformatics Resource Center
706-542-1447 | b...@uga.edu | http://eupathdb.org

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

Peter Meier

unread,
Apr 27, 2010, 3:52:23 AM4/27/10
to puppet...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> [0] I have various nodes defined by function (e.g. database server) that
> inherit from a base node. One thing the base node does is check a fact
> that returns the location of the client. It then includes a module with
> the location-specific configuration, such as the ntp server.

so you include in the base the ntp module and set the $ntp_servers
variable in the sub-node? Yes, this will not work as when the ntp class
is included the ntp_servers are not yet set.

The best practice is to either go with an external node tool for
complexe node definitions or at least not to use any includes in
super-nodes which behavior you want to tweak with variables in
sub-nodes, as inheritance in nodes don't work the way most people expect it.

cheers pete

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkvWl7YACgkQbwltcAfKi3/cQgCeIiiI7BrLadADj/nN60wn5vNe
9LkAn2v4r6jBgChk0HsI2/kCZUU24SH6
=uiQL
-----END PGP SIGNATURE-----

Bernd Adamowicz

unread,
Apr 27, 2010, 4:40:57 AM4/27/10
to puppet...@googlegroups.com
Sounds pretty much like the problem I posted yesterday. You may have a look at it here: http://groups.google.com/group/puppet-users/browse_thread/thread/336a6c1d941acca8/b50faf7711b7bf41?hl=en&lnk=gst&q=adamowicz#b50faf7711b7bf41.

It seems that external node definitions and different environments are the solution for our problems. Just search the Puppet-Wiki and you'll get the information on theses topics.

Bernd

> -----Ursprüngliche Nachricht-----
> Von: puppet...@googlegroups.com [mailto:puppet-
> us...@googlegroups.com] Im Auftrag von Brian Pitts
> Gesendet: Dienstag, 27. April 2010 03:54
> An: puppet...@googlegroups.com
> Betreff: [Puppet Users] Setting Variable in Class or Node and Using in
> Included Module
> users+un...@googlegroups.com.

Dan Carley

unread,
Apr 27, 2010, 5:48:19 AM4/27/10
to puppet...@googlegroups.com
On 27 April 2010 08:52, Peter Meier <peter...@immerda.ch> wrote:
The best practice is to either go with an external node tool for
complexe node definitions or at least not to use any includes in
super-nodes which behavior you want to tweak with variables in
sub-nodes, as inheritance in nodes don't work the way most people expect it.

Or similarly move the configuration data out of your manifests using RIP's extlookup[0].

I use the precedence to set NTP servers on a default, per-domain or per-host (less common) basis.

Brian

unread,
Apr 27, 2010, 9:09:44 AM4/27/10
to Puppet Users


On Apr 27, 3:52 am, Peter Meier <peter.me...@immerda.ch> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> > [0] I have various nodes defined by function (e.g. database server) that
> > inherit from a base node. One thing the base node does is check a fact
> > that returns the location of the client. It then includes a module with
> > the location-specific configuration, such as the ntp server.
>
> so you include in the base the ntp module and set the $ntp_servers
> variable in the sub-node? Yes, this will not work as when the ntp class
> is included the ntp_servers are not yet set.
>

I think I understand why that wouldn't work, but that's not what I
tried. I set the variable and include the module at the same level, as
in the example I posted. These two lines are always in the same file.

In my first attempt, the way it worked was I had this in my base node.

case $location {
foo: { include foo::base }
bar: { include bar::base }
default: { error("Cannot determine at what university $fqdn is
located") }
}

and then in foo::base I had

include ntp

since that location was using the default value, and in bar::base I
had

$ntp_servers = ["timeserver1.example.edu", "timeserver2.example.edu"]
include ntp

since it was using a custom value. However, the value of $ntp_servers
wasn't being set properly for hosts at location bar, hence my post.

In my second attempt I moved everything related to ntp out of
foo::base and bar::base modules and into the base node, like so

include ntp
case $location {
foo: {
include foo::base
}
bar: {
$ntp_servers = ["timeserver1.example.edu",
"timeserver2.example.edu", "timeserver3.bar.edu"]
include bar::base
}
default: {
error("Cannot determine at what location $fqdn is located")
}
}

However, that still did not work. Should either of these have worked?
Why or why not? Could either be tweaked to work?

All the best,
Brian Pitts
Reply all
Reply to author
Forward
0 new messages