On Sunday, November 29, 2015 at 10:09:20 PM UTC-6, François Lafont wrote:
>
> Hi, 
>
> I'm using Puppet 4 and I'm wondering if this (see below) is possible. 
>
> I have 2 Puppet modules, moda and modb. We can imagine that theses modules 
> have just one class init.pp.



"init.pp" is the name of a manifest file, not of a class.  Moreover, 
although it's poor form, such a manifest could contain definitions for more 
than one class.  Moreover, in 

 

> The class ::moda has a parameter "param" with 
> the default value defined in the code of "./moda/functions/data.pp".



"./moda/functions/data.pp" appears to be the name of another manifest 
file.  I suppose it's instead supposed to be a Ruby (.rb) file defining a 
custom function, and although I can speculate based on its name what it 
might do, the details matter, and you have not provided them.


The 
> module modb depends on the module moda (the dependency is indicated in 
> ./modb/metadata.json) and the class ::modb have the parameter "param" too. 
>
> I would like to define the default value of the parameter ::modb::param 
> to ensure that it is equal to the value of ::moda::param, ie I would like 
> to have this: 
>
> a) If the value of ::moda::param is defined in hiera or in 
> environment.conf 
> with "moda::param: xxxx", then the default value of ::modb::param is xxxx 
> too. 
>
> b) If the value of ::moda::param is not defined at all (in hiera, in 
> environment.conf 
> etc.), then the default value of ::modb::param is 
> moda::data()['moda::param']. 
>
>
$::moda::param *has no value* until and unless class ::moda is declared, 
and there is no reliable way to anticipate what value that parameter would 
have if its class were declared.  (You might anticipate and emulate some of 
the ways in which a value could be bound to that parameter, but there are 
others that simply cannot be anticipated.)  Once the class is declared, on 
the other hand, the only reasonable way to get the value of any of its 
parameters is to do so directly (i.e. as $::moda::param).

 

> Is there a proper way to do that with Puppet 4? 
>


I'm inclined to doubt that what you suggest is proper at all.  It seems 
like an awfully tight coupling between modules.  If class ::modb depends on 
class ::moda to be applied to to the target node (as opposed to depending 
only on data and maybe functions provided by module moda) then you can hack 
it together this way:

class modb inherits ::moda ($param = $::moda::param) {
  # ...
}

That's a variation on the "params class" pattern, which has been slowly 
falling out of favor.  You could also interpose a genuine params class 
(modb::params) between classes ::moda and ::modb.  In any event, this 
(anti?)pattern relies on a side effect of class inheritance to ensure that 
the parent class is evaluated before the child class, which in your case 
you need to ensure in order for one of moda's parameter values to be used 
directly as a default value of one of modb's parameters.  This does give 
you increased exposure to the general Puppet constraint that if you ever 
use the resource-like syntax for a class declaration (i.e. of class ::moda) 
then that must be the first declaration of that class that is evaluated.

If you want to use $::moda::param in class ::modb in pretty much any other 
way than as a class parameter default, then you don't need and should not 
use class inheritance.  For instance, this is an old-school approach that 
might be useful to you:

class modb inherits ::moda ($param = 'NOT GIVEN') {
  include '::moda'

  $real_param = $param ? {
    'NOT GIVEN' => $::moda::param,
    default => $param
  }

  # use $real_param in what follows ...
}


John

-- 
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/dc64420a-e732-4689-b3b5-6b35b091353f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to