'inherits' in a defined type

545 views
Skip to first unread message

Jonathan Gazeley

unread,
Sep 3, 2014, 8:58:46 AM9/3/14
to puppet...@googlegroups.com
Hopefully this is just me having a bad day, but I'm having trouble
writing a new module.

I have a mymodule::params class with some sensible site defaults in it,
and the other classes "inherit mymodule::params".

I also have some defined types, and of course you can't use 'inherits'
in a defined type. So how do I access site variables in my defined
types? I tried using 'include mymodule::params' but this didn't provide
the variables either.

Thanks,
Jonathan

Atom Powers

unread,
Sep 3, 2014, 10:05:20 AM9/3/14
to puppet...@googlegroups.com
You may need to define local versions of them that equal the param or default to the param.

$var = $::class::params::var




--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/54071074.9010504%40bristol.ac.uk.
For more options, visit https://groups.google.com/d/optout.



--
Perfection is just a word I use occasionally with mustard.
--Atom Powers--

Jonathan Gazeley

unread,
Sep 3, 2014, 11:23:15 AM9/3/14
to puppet...@googlegroups.com
Thanks - this works. Seems a bit inelegant, but it'll have to do.

Cheers,
Jonathan



On 03/09/14 15:05, Atom Powers wrote:
You may need to define local versions of them that equal the param or default to the param.

$var = $::class::params::var
On Wed, Sep 3, 2014 at 5:58 AM, Jonathan Gazeley <Jonathan...@bristol.ac.uk> wrote:
Hopefully this is just me having a bad day, but I'm having trouble writing a new module.

I have a mymodule::params class with some sensible site defaults in it, and the other classes "inherit mymodule::params".

I also have some defined types, and of course you can't use 'inherits' in a defined type. So how do I access site variables in my defined types? I tried using 'include mymodule::params' but this didn't provide the variables either.

Thanks,
Jonathan


--
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...@googlegroups.com.



--
Perfection is just a word I use occasionally with mustard.
--Atom Powers--
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAF-H%3DO%3DBaKsd1AWmTFmahUSa473fj5N1rgtfO6MFrY%2Bjs7vGcQ%40mail.gmail.com.

jcbollinger

unread,
Sep 4, 2014, 12:10:13 PM9/4/14
to puppet...@googlegroups.com


On Wednesday, September 3, 2014 9:05:20 AM UTC-5, Atom Powers wrote:
You may need to define local versions of them that equal the param or default to the param.

$var = $::class::params::var



Note that the point of the pattern of inheriting params classes is only to ensure that the inherited class is evaluated before the inheriting class, so that you can rely on the variables in question to have been defined in time for use as parameter defaults for the inheriting class.  There is no other good reason to use it.

In particular, it is poor form to inherit a class for the purpose of referring to its variables via unqualified names; you should always refer to the variables of any other class via their qualified names, even if the current class inherits from the variables' host class.

Also, where you don't need your params class for class parameter defaults, you should 'include' it instead of inheriting from it.

If you want a local version of a(nother) class's variable (maybe for conveniece with templates) then you should create one in exactly the way AP described (but don't forget to 'include' the params class).  For most purposes, however, you can just refer to the foreign variable via its qualified name.


John

Felix Frank

unread,
Sep 15, 2014, 10:48:55 AM9/15/14
to puppet...@googlegroups.com
On 09/04/2014 06:10 PM, jcbollinger wrote:
> You may need to define local versions of them that equal the param
> or default to the param.
>
> $var = $::class::params::var
>
>
>
> Note that the point of the pattern of inheriting params classes is only
> to ensure that the inherited class is evaluated before the inheriting
> class, so that you can rely on the variables in question to have been
> defined in time for use as parameter defaults for the inheriting class.
> There is no other good reason to use it.

Specifically, using it in any other context is likely plain wrong.

(Note that I've never been a user or proponent of the "inherit params
class" pattern, but it has long been the only safe choice for people who
like params class style definitions of default values - I acknowledge that.)

Since the variables from the params class are expected to be used only
as default values, the module interface will usually allow the node to
override them by specifying other values to the inheriting class, either
through
1) resource style class declaration or
2) binding of Hiera values.

When using values from the params class in other contexts, such as
defined types, those contexts will assume that those defaults are in
effect for the current node, which is no safe assumption.

Felix
Reply all
Reply to author
Forward
0 new messages