Le 17/09/2014 10:14, Francois Lafont a écrit :
> For instance with:
> 
> class my_module (
>   $var1 = $my_module::params::var1,
>   $var2 = $my_module::params::var2,
>   $var3 = $my_module::params::var3,
> ) inherits my_module::params {
>     # The body of the class
> }  
> 
> and:
> 
> class my_module::params {
> 
>   ## API of the module ##
>   $var1 = default_value1
>   $var2 = default_value2
>   $var3 = default_value3
> 
>   ## shared variables between the classes of my_module
>   ## but they are not part of the API module
>   $foo1 = default_value_foo1
>   $foo2 = default_value_foo2
>   $foo3 = default_value_foo3
>   ...
> }
> 
> Imagine I want to define an inner class "internal" which
> needs to use $var2, $var3, $foo1 and $foo2. Which pattern
> should I use to define my "internal" class.
> 
> This one?
> 
> class my_module::internal (
>   $var2,
>   $var3,
> ) inherits my_module::params {
> 
>   notify { "value of foo1 -> $foo1": }
>   notify { "value of foo2 -> $foo2": }
> 
> } 
> 
> This one?
> 
> class my_module::internal (
>   $var2,
>   $var3,
> ) {
>   
>   require my_module::params
>   notify { "value of foo1 -> $my_module:params::foo1": }
>   notify { "value of foo2 -> $my_module:params::foo2": }
> 
> } 
> 
> Or maybe another one...?

In fact, It's curious. I have made some tests with puppet 3.7.0
on Debian Wheezy and I can simply do this:

class my_module::params {

  ## API of the module ##
  $var1 = default_value1
  $var2 = default_value2

  ## shared variables between the classes of my_module
  ## but they are not part of the API module
  $foo1 = default_value_foo1
  $foo2 = default_value_foo2
  ...
}

class my_module (
  $var1 = $my_module::params::var1,
  $var2 = $my_module::params::var2,
) inherits my_module::params {

  include my_module

} 

class my_module::internal {

  file { '/tmp/test_internal.txt':
    content => template('test/test_internal.txt.erb'),
  }

  notify { 'm1': message => "var1 = ${my_module::var1}", }
  notify { 'm2': message => "var2 = ${my_module::var2}", }
  notify { 'm3': message => "foo1 = ${my_module::foo1}", }
  notify { 'm4': message => "foo2 = ${my_module::foo2}", }

}

It's curious because I use no inheritance in my_module::internal,
no require etc. and despite that I can access to the variables of
my_module. Is it normal?

One thing very curious too: in the my_module::internal class
I can access to $var1 with this syntax :

    ${my_module::var1}

and $var1 is empty. Ok, I think I understand that. But in my
template "test_internal.txt.erb" I can use directly:

    blabla <%= @var1 %> blabla

without the fully qualified name. Here, I don't understand.

Anyway, although I do not understand well the "scope" mechanism
etc. I feel that I can just use this design for inner classes :

class my_module::internal {

  # No inheritance, no require of my_module::params etc.
  # It's unnecessary.

  # In the class, I must use the fully qualified name
  # to access to a variable of my_module inherited by
  # my_module::params (and sometimes overwritten).
  # For instance:
  notify { 'm1': message => "var1 = ${my_module::var1}", }

  # But in templates, I can directy use @var1.

}

Am I wrong?

François Lafont

-- 
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 puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/54196F0D.4080909%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to