Include class with parameters

195 views
Skip to first unread message

Harish Kothuri

unread,
Jun 9, 2016, 12:49:58 AM6/9/16
to Puppet Users
Hi,

I have classA with parameter and i want to include this in another classB . I expected to consider the default value defined in foreman for classA but it's not happening. 


class classA ($version)
{
   
  Package { 'Package A' 
      ensure           => 'installed',
      source           => 'C:\install\file_${version}.msi',
  }
}


class classB($variable)
{

include classA   # throws error in this line "Must pass parameter to Class [ClassA]

  Package { 'title B' 
      ensure           => 'installed',
      source           => 'C:\install\file_${variable}.msi',
      require           => Package['Package A'],
  }
}


Can someone help me on this.

Thanks

Martin Alfke

unread,
Jun 9, 2016, 1:15:24 AM6/9/16
to puppet...@googlegroups.com
Hi Harish,
On 09 Jun 2016, at 06:49, Harish Kothuri <harish...@gmail.com> wrote:

> Hi,
>
> I have classA with parameter and i want to include this in another classB . I expected to consider the default value defined in foreman for classA but it's not happening.
>
>
> class classA ($version)
> {
>
> Package { 'Package A'
> ensure => 'installed',
> source => 'C:\install\file_${version}.msi',
> }
> }

You defined a class with a parameter without a default value.
When declaring (include) this class you must pass a value to the parameter by using class as resource type:
e.g.
class { ‘classA’:
version => ‘1.2.3’,
}

>
>
> class classB($variable)
> {
>
> include classA # throws error in this line "Must pass parameter to Class [ClassA]
>
> Package { 'title B'
> ensure => 'installed',
> source => 'C:\install\file_${variable}.msi',
> require => Package['Package A'],
> }
> }
>
>
> Can someone help me on this.
>
> 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/fc54c6ea-276e-41d9-b6c3-93390846f1df%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Jun 9, 2016, 9:14:56 AM6/9/16
to Puppet Users


On Wednesday, June 8, 2016 at 11:49:58 PM UTC-5, Harish Kothuri wrote:
Hi,

I have classA with parameter and i want to include this in another classB . I expected to consider the default value defined in foreman for classA but it's not happening. 


class classA ($version)
{
[...]
}


class classB($variable)
{

include classA   # throws error in this line "Must pass parameter to Class [ClassA]
[...]
}



The Foreman is providing you an opportunity to define a default for the class parameter for its own purposes.  This default applies when you assign a class to a node directly via The Foreman, but it is external to Puppet.  It does not apply when one class assigns another to the node.

You have three main options:
  1. In class B, use resource-like declaration of class A, in which you explicitly specify a value for class A's parameter.
  2. Rely on automated data binding (i.e. Hiera) to specify a value for class A's parameter.
  3. Specify your default value for the parameter in class A's definition.
In fact, those options are non-exclusive: you can use any combination.  They are listed in priority order; the first one that provides a parameter value wins.  For classes assigned directly via The Foreman, any parameter assignments via The Foreman are roughly equivalent to option (1).  Parameter value binding is evaluated on a per-parameter basis, so different parameters' values can be assigned via different options.

HOWEVER, if you have any classes that may be declared more than once then option (1) is largely off the table.  That option can be exercised at most once for any given class, and that must occur in the first-evaluated declaration of the class for a given catalog.  It can be difficult to control the order in which declarations are evaluated, so exercising option (1) safely can be tricky.

Inasmuch as you can have only one default value per parameter built into a class definition, and inasmuch as one prefers to avoid modifying third-party modules, option (3) has somewhat limited utility.  You cannot rely on it (exclusively) where you want a parameter to take different values in different cases, and if you do not like the value you get this way then your only means to change it is to modify the class definition.

That leaves option (2) as the only truly general-purpose approach.  Automated data binding was one of the greatest advances in Puppet 3 -- learn it, live it, love it.


John

Harish Kothuri

unread,
Jun 9, 2016, 10:20:44 AM6/9/16
to Puppet Users
Thank you. Will take a look at "Automated data binding".
Reply all
Reply to author
Forward
0 new messages