Thanks Ohad, I think that got me headed in the right direction. Here's
what I came up with for the custom function (I made it more reusable):

---- snip ----
Puppet::Parser::Functions::newfunction(:array_merge, :type
=> :rvalue, :doc => "A function that accepts a normal string
    and an array. The function returns an array that contains each of
the input
    array's values, merged with the string. Merging is performed by
replacing
    the <VAL> token in the string with the array value. If no <VAL>
token
    exists, the array string is appended to the end of the string.")
do |args|
        token = '<VAL>'
        inputstring = args[0]

        if args[1] then
            outputstrings = Array.new
            args[1].each do |val|
                 if inputstring.match(token) then
                     outputstrings << inputstring.sub(token, val)
                 else
                     outputstrings << inputstring.concat(val)
                 end
            end
        else
            return false
        end

        return outputstrings
end
---- snip ----

In my manifest, I can do this and it works:

---- snip ----
$clustersources = array_merge("puppet://server/module/
configfile.<VAL>", $clusters)

file { "/etc/somefile.conf":
    source => $clustersources;
}
---- snip ----

However, I still haven't figured out how to tack on the default
configfile ("puppet://server/module/configfile.default") to the array.
I tried this, but it doesn't work:

file { "/etc/somefile.conf":
    source => [
        $clustersources,
        "puppet://server/module/configfile.default",
    };
}

Also in some cases, in addition to the default config file, I'll also
need some config files to go before the cluster-specific config files
in the array. I tried using the += operator to append data to the
array, but that doesn't work because everything's in the same scope (I
get the error, "Cannot append, variable sources is defined in this
scope").

Am I still going about this the right way? Thanks to all for any help
you can give me.

On Jan 31, 6:50 pm, Ohad Levy <[email protected]> wrote:
> I guess one way would be to write a small custom function which gets an
> array as an argument and returns an array of sources.
>
> (didnt test :))
>
> module Puppet::Parser::Functions
>   newfunction(:array2source, :type => :rvalue) do |args|
>     if args[0].is_a?(Array)
>       sources = Array.new
>       args[0].each {|s| source << "puppet://server/module/configfile.#{s}"}
>     end
>     sources <<  "puppet://server/module/configfile.default"
>     return sources
>   end
> end
>
> and then in your manifest just do:
>
> file { "/etc/somefile.conf":
>    source => array2source(clusters)
>
> }
>
> cheers,
> Ohad
>
> On Sun, Feb 1, 2009 at 10:25 AM, threetee <[email protected]> wrote:
>
> > My external node classifier returns some arrays in the list of
> > parameters. Example output (names have been changed to protect the
> > innocent):
>
> > $ ./node_classifier a.b.com
> > --- %YAML:1.0
> > "classes": ["class1", "class2", "class3"]
> > "parameters":
> >  "hostname": "a"
> >  "name": "a"
> >  "domain": "b.com"
> >  "fqdn": "a.b.com"
> >  "group": "<host group>"
> >  "clusters": ["cluster1", "cluster2", "cluster3", "cluster4"]
>
> > In my puppet manifests, I would like to be able to use the elements in
> > the clusters array to generate multiple files to use for the source
> > parameter of the file type. For example, given the above YML, I'd like
> > the end result of the file parameter to behave like this:
>
> > file { "/etc/somefile.conf":
> >    source => [
> >        "puppet://server/module/configfile.cluster1",
> >        "puppet://server/module/configfile.cluster2",
> >        "puppet://server/module/configfile.cluster3",
> >        "puppet://server/module/configfile.cluster4",
> >        "puppet://server/module/configfile.default"
> >    ];
> > }
>
> > However, I can't figure out how to generate the multiple sources from
> > the array. This (unsurprisingly) doesn't work:
>
> > file { "/etc/somefile.conf":
> >    source => [
> >        "puppet://server/module/configfile.$clusters"
> >    ];
> > }
>
> > Is what I want to do here possible using the native Puppet language?
> > Will I need to write a custom function to generate the source files
> > myself, then pass them to the file type? Is there some other step that
> > I'm just missing?
>
> > Thanks in advance for any help. Getting this to work will save me a
> > whole lot of time.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to