Puppet parameterized classes

Parameters allow a class to request external data. If a class needs to use data other than facts for configuration, use a parameter for that data.

We can use class parameters as normal variables inside the class definition. The values of these variables are set based on user input when the class is declared, rather than with normal assignment statements.

Include-like resource declarations allow you to declare a class multiple times — but no matter how many times you add the class, it is added to the catalog only once. This allows classes or defined types to manage their own dependencies and allows you to create overlapping role classes, in which a given node can have more than one role.

Let’s first create a parametrized class by creating a my_parameters module

We update-modules/my_parameters/manifests/my_class.pp as shown:

# @summary A parameterized class

#

# A parameterized class

#

# @example

#   include my_parameters::my_class

class my_parameters::my_class (String $greeting) {

    file { '/tmp/my_parameters':

    ensure  => 'present',

    content => $greeting,

    path    => '/tmp/my_parameters',

  }

}

Things to observe:

*This class requires a string parameter $greeting

*The type-definition, String, is optional

We then include the class in our node definition as we have done in the past, manifests/site.pp:

$my_content = 'Hello Top'

node default {

  include my_module

  include my_variables

  include my_facts

  include my_parameters

}

Things to observe:

*We did not supply a value for the $greeting parameter as include-like declarations do not support passing parameters

*Because one can include the same class multiple times using include-like declarations; it makes sense that they do not support passing parameters (otherwise one could introduce discrepancies)

*We have a problem as we have not supplied a value for the required $greeting parameter yet.




Relevant Blogs:

Puppet schedule 

Puppet loops 

Puppet service resource 

AWS CodeBuild: A Fully Managed Build Tool

Recent Comments

No comments

Leave a Comment