How can I send parameters to a required class?

979 views
Skip to first unread message

Francis Chan

unread,
Aug 27, 2015, 7:44:17 AM8/27/15
to Puppet Users
I'm using the something like this since I want all of class bar to be applied before any class foo:

# init.pp for module bar
class bar( $var = "hello" )
{
}


# something.pp in module foo
class foo::something {
   
require bar
}

Is it possible to change bar::var using when foo uses the "require" statement?  Is there another way that will keep the same dependency but allow me to pass in parameters to the required class?

Thanks!

Peter Huene

unread,
Aug 27, 2015, 8:59:28 AM8/27/15
to puppet...@googlegroups.com
On Wed, Aug 26, 2015 at 11:50 PM, Francis Chan <poi...@gmail.com> wrote:
I'm using the something like this since I want all of class bar to be applied before any class foo:

# init.pp for module bar
class bar( $var = "hello" )
{
}


# something.pp in module foo
class foo::something {
   
require bar
}


Instead of using the `require` function, try using the "resource-like" declaration syntax:

class { bar:
  var => world,
  before => Class[foo::something]
}

This has the same effect as the require function (i.e. making Class[foo::something] dependent upon Class[bar]), but it passes "world" as the value for $var.

Is it possible to change bar::var using when foo uses the "require" statement?  Is there another way that will keep the same dependency but allow me to pass in parameters to the required class?

Thanks!

--
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/23998ab8-35c4-4bc8-987a-41bc791ade00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--
Peter Huene
Software Engineer, Puppet Labs
Puppet Open Source Team
-------------------------------------------

PuppetConf 2015 is coming to Portland, Oregon! Join us October 5-9!

jcbollinger

unread,
Aug 28, 2015, 9:12:40 AM8/28/15
to Puppet Users


On Thursday, August 27, 2015 at 7:59:28 AM UTC-5, Peter Huene wrote:

On Wed, Aug 26, 2015 at 11:50 PM, Francis Chan <poi...@gmail.com> wrote:
I'm using the something like this since I want all of class bar to be applied before any class foo:

# init.pp for module bar
class bar( $var = "hello" )
{
}


# something.pp in module foo
class foo::something {
   
require bar
}


Instead of using the `require` function, try using the "resource-like" declaration syntax:



Oh, goodness No!  Avoid resource-like class declarations wherever possible, and always avoid using them to declare public classes of any module.  Although resource-like syntax does provide for specifying parameters at the point of declaration, it introduces both an evaluation-order dependency and a potential declaration conflict.

The best way to provide class parameter values, especially for public classes, is via Puppet's automatic data binding feature.  If you're using an ENC, then providing class parameters via ENC is also a reasonable alternative.  The manual discusses class declarations and the include-like (which includes 'require') vs. resource-like styles.  Do not overlook the best-practices callout in that section.  There are cases where using resource-like syntax makes sense, but simply wanting to provide non-default parameter values is in no way good justification.

Automated data binding relies on Hiera.  A full run-down on Hiera use is a little out of scope here, but basically, when you declare any class, using either style, Puppet will perform a Hiera lookup for each class parameter that is not assigned a value in the declaration (for include-like syntax, that's all of them).  It uses keys of the form "full::class::name::parameter", and if that key is present then its value is used as the value for the class parameter.  In your example, then, you would arrange for Hiera to provide a value for key "bar::var".


John

Reply all
Reply to author
Forward
0 new messages