robert.gstoehl
2010-Mar-08 14:48 UTC
[Puppet Users] $title as default value for argument in defintion
Hey there, I''d like to provide a definition with an argument which is optional and defaults to the title: net::route_default{"first": gateway => "192.168.0.1"} or the shortcut: net::route_default{"192.168.0.1"} class net { define route_default($gateway=$title) { exec {"echo $gateway >> /etc/defaultrouter": unless => "grep $gateway /etc/defaultrouter" } ... } node foo { net::route_default{"192.168.0.1"} } trying it with noop: notice: //Node[myhost.local]/Net::Route_default[192.168.0.1]/Exec[echo myhost.local >> /etc/defaultrouter]/returns: is notrun Funny - puppet substitutes $title with the fully qualified hostname - can someone shed some light on this one? :) Thanks & cheers Robert -- 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+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Thomas Bellman
2010-Mar-08 15:19 UTC
Re: [Puppet Users] $title as default value for argument in defintion
robert.gstoehl wrote:> I''d like to provide a definition with an argument which is optional > and defaults to the title: > > net::route_default{"first": gateway => "192.168.0.1"} > or the shortcut: > net::route_default{"192.168.0.1"}Do it like this: define route_default($gateway="") { if $gateway == "" { $xgateway = $title } else { $xgateway = $gateway } # Use $xgateway here instead of $gateway }> define route_default($gateway=$title) { > exec {"echo $gateway >> /etc/defaultrouter": > unless => "grep $gateway /etc/defaultrouter" > } > ... > Funny - puppet substitutes $title with the fully qualified hostname - > can someone shed some light on this one? :)The default values for definition parameters are evaluated when the definition is defined, not when the definition is used. And when you are outside of definitions, $title is set to the node name. /Bellman -- 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+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
robert.gstoehl
2010-Mar-08 16:36 UTC
[Puppet Users] Re: $title as default value for argument in defintion
makes sense, thanks Robert On Mar 8, 4:19 pm, Thomas Bellman <bell...@nsc.liu.se> wrote:> robert.gstoehl wrote: > > I''d like to provide a definition with an argument which is optional > > and defaults to the title: > > > net::route_default{"first": gateway => "192.168.0.1"} > > or the shortcut: > > net::route_default{"192.168.0.1"} > > Do it like this: > > define route_default($gateway="") > { > if $gateway == "" { > $xgateway = $title > } else { > $xgateway = $gateway > } > # Use $xgateway here instead of $gateway > } > > > define route_default($gateway=$title) { > > exec {"echo $gateway >> /etc/defaultrouter": > > unless => "grep $gateway /etc/defaultrouter" > > } > > ... > > Funny - puppet substitutes $title with the fully qualified hostname - > > can someone shed some light on this one? :) > > The default values for definition parameters are evaluated when the > definition is defined, not when the definition is used. And when > you are outside of definitions, $title is set to the node name. > > /Bellman-- 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+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.