Dynamically create arguments for a define (or parametrized class)

662 views
Skip to first unread message

Alessandro Franceschi

unread,
Oct 27, 2011, 7:29:58 AM10/27/11
to puppet...@googlegroups.com
Maybe I'm asking too much, but is there a way to dynamically add resources in a define/parametrized class based on a variable (or hash) passed to a containing class/define?

Something that when I call
boo { "bah":
    options  => {
      "optiona"   => "valuea",
      "optionb"   => "valueb",
    },
}

gets this result (what follows is the wanted behaviour not the actual code of the boo define)

define boo (
    $options
    ) {

   bar { "name":
      optiona => valuea,
      optiona => valuea,
   }
}

Or a more general note, is it possible to manage dynamically the name and presence of arguments in a define?

Any help or direction is welcomed.
Al

Henrik Lindberg

unread,
Oct 27, 2011, 11:03:42 AM10/27/11
to puppet...@googlegroups.com
Did you try something like:

define boo($a="a", $b="b", $c="c") {
bar { $title:
a => $a,
b => $b,
c => $c,
}
}

boo { 'the title' : a => "a value", c => "c value" }

- henrik

> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/puppet-users/-/5PkidQ_83E0J.
> 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.


Alexandre

unread,
Oct 27, 2011, 1:01:14 PM10/27/11
to Puppet Users
There is a function to dynamically create Puppet resources from a
hash: createresources(): http://docs.puppetlabs.com/references/stable/function.html#createresources


On 27 oct, 17:03, Henrik Lindberg <henrik.lindb...@cloudsmith.com>
wrote:

Alessandro Franceschi

unread,
Oct 27, 2011, 1:43:10 PM10/27/11
to puppet...@googlegroups.com


On Thursday, October 27, 2011 4:03:42 PM UTC+1, Henrik Lindberg wrote:
Did you try something like:

define boo($a="a", $b="b", $c="c") {
     bar { $title:
       a => $a,
       b => $b,
       c => $c,
    }
}

boo { 'the title' : a => "a value", c => "c value" }

Thanks for reply, Henrik
What you've written works, but doesn't fit my case.
Taken your examples I don't want (can't) pre-define boo arguments.
I would have something like:
define boo($options) {

bar { $title:
       a => $a, # this argument dynamically created according to $options (which is supposed to be an hash)
       b => $b, # as above
       c => $c, # as above
    }
}

I would use boo in this way:
boo { "title":
   options {
       a => aa,
       b => bb,
       c => dfs,
   },
}

... (am I asking too much ? :-)

Alessandro Franceschi

unread,
Oct 27, 2011, 1:46:08 PM10/27/11
to puppet...@googlegroups.com
Thanks for the link.
I fear that this function doesn't exactly apply to my case (it creates dynamically resources, not arguments for a resource) but I should give it a deeper look and consider if I can work around it.

al

Ken Barber

unread,
Oct 27, 2011, 1:51:06 PM10/27/11
to puppet...@googlegroups.com
Yeah - nice one Henrik.

Al - You should be able to create something based on create_resources
to do what you want. Its a similar concept.

ken.

> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/puppet-users/-/IRS5R_gfpk0J.

jcbollinger

unread,
Oct 28, 2011, 12:01:06 PM10/28/11
to Puppet Users


On Oct 27, 12:43 pm, Alessandro Franceschi <a...@lab42.it> wrote:
> I would have something like:
> define boo($options) {
>
> bar { $title:
>        a => $a, # this argument dynamically created according to $options
> (which is supposed to be an hash)
>        b => $b, # as above
>        c => $c, # as above
>     }}
>
> I would use boo in this way:
> boo { "title":
>    options {
>        a => aa,
>        b => bb,
>        c => dfs,
>    },
>
> }
>
> ... (am I asking too much ? :-)


I suspect that your intention is not coming across well. Are you just
asking how to get values out of a hash? For instance, what about
this:

define boo($options) {
bar { $title:
a => $options['a'],
b => $options['b'],
c => $options['c']
}
}

boo { "title":
options => {
a => 'aa',
b => 'bb',
c => 'dfs',
}
}

Note that in that particular usage, create_resources() can replace
definition 'boo' altogether:

create-resources('bar', { 'title' => { 'a' => 'aa', 'b' => 'bb', 'c'
=> 'cc' }})

Note that the inner hash in that case is exactly what you were
assigning to definition 'boo's 'options' parameter. This depends on
the keys of the inner hash(es) all being names of parameters of the
resource type 'bar'.


John
Reply all
Reply to author
Forward
0 new messages