On Jan 17, 2009, at 12:31 AM, Scott wrote:

>
> So I need a variable in a child class to be visible to the parent
> class and I'm wondering how I can do that?  The docs say "You will
> almost always find that you can avoid resetting variable values using
> the built in conditionals:" but I guess this is one of those cases
> where it just isn't possible:
>
> node default {}
>
> node testserver inherits default {
>  include testclasses                     <- (this is where I need
> $var to be visible)
> }
>
> node test1.example.com inherits testserver {
>  $var = "host server name"
> }
>
> As far as I can tell, there's no way to include another node,
> otherwise I could have "test1.example.com" include "testserver"
> instead of inheriting.  I can't set a variable in the parent class
> from the child class because variables in puppet can only be set
> once.  And, the way it is right now, exactly as it is above, $var
> evaluates to nil because the $var declaration is out of the scope of
> the parent class.
>
> I don't want to include "testclasses" in "test1.example.com" because
> then I'm going to have to include it for every individual "testserver"
> node defeating the whole purpose of inheritance, cluttering up the
> config files and making it easier to make mistakes.  So far, this is
> the only way I've been able to get it to work but it's a kludge and
> I'd prefer to avoid it.
>
> I tried something like this as well:
>
> node default {}
>
> node testserver inherits default {
>  $var = ["text1", "text2"]
> }
>
> node test1.example.com inherits testserver {
>  $var += ["text3"]
> }
>
> but the parameter of the puppet type I'm trying to set with $var
> (nagios_host: hostgroups) doesn't accept arrays.  So if a child class
> can append to an array in a parent class, is there a way to get the
> string version of an array with all the elements concatenated together
> with a "," in between each element?  Either that or is there a way I
> can get the "hostgroups" parameter in the "nagios_host" type to accept
> arrays?
>
> I feel like I've got to be missing something here.  Thanks.

The only real way to do this is to use an external nodes tool.  I've  
attached a simple script that serves node infromation out of YAML  
files; it isn't awesome but it'll do what you want, anyway.

-- 
All power corrupts, but we need the electricity.
     -- Unknown
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com

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

#!/usr/bin/ruby

require 'yaml'

BASEDIR = Dir.chdir(File.dirname(__FILE__) + "/..") { Dir.getwd }
YAMLDIR = File.join(BASEDIR, "yaml")

# Read in a pure yaml representation of our node.
def read_node(node)
    nodefile = File.join(YAMLDIR, "#{node}.yaml")
    if FileTest.exist?(nodefile)
        return YAML.load_file(nodefile)
    else
        raise "Could not find information for %s" % node
    end
end

node = ARGV[0]

info = read_node(node)

# Iterate over any provided parents, merging in there information.
parents_seen = []
while parent = info["parent"]
    raise "Found inheritance loop with parent %s" % parent if parents_seen.include?(parent)

    parents_seen << parent

    info.delete("parent")

    parent_info = read_node(parent)

    # Include any parent classes in our list.
    if pclasses = parent_info["classes"]
        info["classes"] += pclasses
        info["classes"].uniq!
    end

    # And inherit parameters from our parent, while preferring our own values.
    if pparams = parent_info["parameters"]
        # When using Hash#merge, the hash being merged in wins, and we
        # want the subnode parameters to be the parent node parameters.
        info["parameters"] = pparams.merge(info["parameters"])
    end

    # Copy over any parent node name.
    if pparent = parent_info["parent"]
        info["parent"] = pparent
    end
end

puts YAML.dump(info)

Reply via email to