Through the help of Don and Evan, I've figured this out.  My solution is
as follows:

nodes.pp:
import "modules"
import groups/*

node 'admin01.dev.example.com' {
        $webserver = "role"
        include admin
}

groups/admin.pp:

class admin {
        include example
}

modules/examples/manifests/init.pp:

class example {
        if $webserver{
                $webserver = true
        } else {
                $webserver = false
        }
        file { "example_file":
                        path  => "/tmp/example.file",
                        owner => "root",
                        group => "root",
                        mode  => 644,
                        content => template("example.erb"),
        }
}

templates/example.erb:
All machines get this text.
<% if webserver == true -%>
Then insert this text, too.
<% end -%>

Things to note: due to scoping rules, your node must *include*, not
inherit, the class that contains the module that has the template in it.
I.e., in my example, if admin01 inherited the dev class, this does not
work.  Presumably, somebody, I'll understand the scoping rules enough to
know why that is.  Today... is not that day.

Also, the $webserver variable *must* be set before "include"ing the
class that contains the template.  Otherwise, see previous paragraph.

And yes, you could set the $webserver variable to whatever you wish, but
setting it to "role" helps keep things clear in my configs.  And no, I
don't understand why the example class can see that the $webserver
variable exists, but the template can't unless I re-set it.  See
two-paragraphs up.

Finally, you can set $webserver at the "group" (class) level, in - say -
groups/admin.pp, as long as you follow the "set the variable first"
rule.  In this case, you would remove that line from the node
definition, and change admin.pp to the following:

class admin {
        $webserver = "role"
        include example
}

Hope that helps somebody else.

David Bishop

P.S. Keyword dump, to help searchers in the future hit this post:
tagging, special case, inheritance, override configuration, 
hierarchies, how do I make this stupid thing work? 

On Tue, Jun 23, 2009 at 01:56:44PM -0600, David Bishop wrote:
> I know I'm missing something very basic, but I've been beating my head
> on this for hours now without making progress.  I'm trying to 'tag'
> nodes with various values, to be pulled later in some home-grown
> modules.  For instance, I want to mark machines as being a webserver,
> and then in my yum repo module, I have a template that checks for
> webservers and adds a custom repo if true.  I'm very flexible in the 
> details - I don't care if it's a custom variable, a tag, a fact, 
> whatever.  The only thing I *don't* want to do is use inheritance and 
> groups to do this - any given machine will be tagged with various 
> different things, and trying to maintain groups would quickly become 
> unmanageable.  And unfortunately, this isn't something that can be
> discovered automatically using facter, at least as far as I can tell.
> 
> An example of a failed attempt would be (all files stripped down, but
> the meat is there):
> 
> nodes.pp:
> import "modules"
> import groups/*
> import roles/*
> 
> node 'admin01.dev.example.com' inherits admin { include webserver }
> 
> roles/webserver.pp:
> 
> class webserver {
>       $webserver = true
> }
> 
> templates/repo.erb:
> 
> The value of the variable is <%= $webserver::webserver %>.
> 
> 
> I also tried $::webserver (after moving it out of the class definition),
> $webserver, setting a tag in the class and then reading out the defined
> tags, and more.  But that should give you guys an idea of what I'm
> aiming to do.
> 
> Thanks for any pointers!
> 
> David


Attachment: signature.asc
Description: Digital signature

Reply via email to