Hi,

Sorry for hijacking this thread, but it caught my interest. 

My scenario is that I'd like to re-use the title of an nginx server instance 
in, say, the log file for that server instance. However, since I don't want to 
touch the nginx module itself, it seems I have to wrap its server class in one 
of my own to allow setting this kind of defaults - but I have found no way to 
use $title in this way.

The best would be if I could do something like this - assuming nginx::server is 
a module class already defined:
nginx::server {
  default:
    $access_log => "${nginx::logdir}/${mytitle}.log",
    ...,
  ;
  'my-fine-443-server':
    listen_port => 443,
  ;
}

Here it would also be helpful if I could somehow re-use the default values in 
the individual instances too - I might not know what the default values are, 
but I would know what to do with them (append '.log' for instance, or set 
listen_port to the same value as ssl_port or vice versa).

Even being able to do the following would be better than what we're currently 
doing, which is repeating the fully-typed access log line (and all the other 
similar entries) for every instance:
nginx::server { 'my-fine-443-server':
    $access_log => "${nginx::logdir}/${mytitle}.log",
    listen_port => 443,
}

Not sure how I could use functions here either, as I want this to happen at 
instantiation time, not in the module itself.

Am I hoping for too much? Missed something?

/Eirik

> On 7 Oct 2018, at 11:35, Henrik Lindberg <[email protected]> wrote:
> 
> If you are on a reasonably modern Puppet version you should do it like this:
> 
> class myclass(
>  String $base_dir,
>  Optional[String] $conf_dir = "${base_dir}/conf"
> ) {
> }
> 
> I tested it as well:
> 
>  class myclass(
>    String $base_dir,
>    Optional[String] $conf_dir = "${base_dir}/conf"
>  ) {
>    notice "base_dir = ${base_dir}, conf_dir = ${conf_dir}"
>  }
>  class { myclass: base_dir => 'yay' }
> 
> With the result:
> 
>  Notice: Scope(Class[Myclass]): base_dir = yay, conf_dir = yay/conf
> 
> And when executed like this:
> 
>  class myclass(
>    String $base_dir,
>    Optional[String] $conf_dir = "${base_dir}/conf"
>  ) {
>    notice "base_dir = ${base_dir}, conf_dir = ${conf_dir}"
>  }
>  class { myclass: base_dir => 'yay', conf_dir => 'not yay' }
> 
> The result is:
> 
>  Notice: Scope(Class[Myclass]): base_dir = yay, conf_dir = not_yay
> 
> Which I think is what you wanted.
> 
> If the logic you need for coming up with a default value is complex, it can 
> be written as a function to which you present the input as arguments. The 
> above could have been written:
> 
> function mymodule::conf_default(String $base) { "${base}/conf" }
> class myclass(
>  String $base_dir,
>  Optional[String] $conf_dir = mymodule::conf_default($base_dir)
> ) {
> }
> 
> Which for the case you showed is total overkill, but good to know if
> you need something more complex in another place in your code.
> 
> Hope this helps.
> Best,
> - henrik
> 
> 
> 
>> On 2018-10-06 18:15, 'Dan White' via Puppet Users wrote: > You need to do 
>> like this:
>> class myClass (
>> String $base_dir,
>> Optional[String] $conf_dir,
>> ) {
>>     if $myClass::conf_dir == undef {
>>       $myClass::actual_conf_dir = "$myClass::base_dir/conf”
>>     } else {
>>         $myClass::actual_conf_dir = $myClass::conf_dir
>>     }
>>     … and then use $myClass::actual_conf_dir in the template
>> }
>>> On Oct 3, 2018, at 12:41 PM, Jody Des Roches <[email protected]> wrote:
>>> 
>>> I'd like to set default values for parameters that will be passed to epp 
>>> templates.  However, the default value is based on another parameter.  I 
>>> understand that variables are immutable but this is a parameter that 
>>> shouldn't be touched unless it wasn't set.
>>> 
>>> Here is an example construct with a few of my syntax attempts.
>>> 
>>> class myClass (
>>> String $base_dir,
>>> Optional[String] $conf_dir,
>>> ) {
>>> #Attempt 1: Failed
>>> if $myClass::conf_dir == undef { $myClass::conf_dir = 
>>> "$myClass::base_dir/conf" }
>>> 
>>> #Attempt 2: Failed
>>> if !$myClass::conf_dir { $myClass::conf_dir = "$myClass::base_dir/conf" }
>>> 
>>> #Attempt 3: Failed
>>> unless $myClass::conf_dir { $myClass::conf_dir = "$myClass::base_dir/conf" }
>>> }
>>> 
>>> -- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "Puppet Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to [email protected].
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/puppet-users/8e2db8c1-7353-4360-adc5-00713e1c0214%40googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
> 
> 
> -- 
> 
> Visit my Blog "Puppet on the Edge"
> http://puppet-on-the-edge.blogspot.se/
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/puppet-users/ppcjse%24qmf%241%40blaine.gmane.org.
> For more options, visit https://groups.google.com/d/optout.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/19FFCFB6-B348-485D-8D1C-377AFD2FC79F%40anduin.net.
For more options, visit https://groups.google.com/d/optout.

Reply via email to