0
votes

In the scope of one class, I need to be able to access a variable from another class. The variable is passed as a parameter, e.g.

class parameterized_class (
    $param1,
) {
    ...
}

and

class other_class () {
    include parameterized_class
    Class['parameterized_class'] -> Class['other_class']
    $local_var = $parameterized_class::param1
}

With an example usage of:

node default {
    class { 'parameterized_class':
        param1 => 'somevalue',
    }
    class { 'other_class': }
}

The above example does not work, as I get an error that looks roughly like:

Must pass param1 to Class[Parameterized_class] at /path/to/modules/parameterized_class/manifests/init.pp:1 on node localhost

Obviously, include is trying to declare parameterized_class without passing any parameters. But from the docs, I can see that include allows for a class to have been already previously declared, and since I have the parameterized_class declaration as a dependency of other_class, I don't understand how I could be getting this error.

I'm using Puppet 3.4.3, the version available on Ubuntu 14.04 Trusty

How should I go about retrieving the value of the $param1 in parameterized_class from within other_class's scope? Is it not possible to use include on parameterized classes?

1
First, upgrade to a non-obsolete version of Puppet: docs.puppet.com/puppet/4.10/…. Second, you can use include with parameterized classes, but that is not going to help you achieve what you want here. Third, having Class['parameterized_class'] -> Class['other_class'] inside the other_class definition with the way you are trying to do things is probably going to end up causing problems eventually. Given this and what you are trying to do, the easiest ways to fix your problem is either hiera or remove the include parameterized_class line. - Matt Schuchard
Let us know which route you prefer and we can help further. - Matt Schuchard

1 Answers

0
votes

According to the Puppet documentation (https://docs.puppet.com/puppet/3.5/lang_classes.html#include-like-behavior) you cannot use include-like declarations with mandatory parameters. In your case, what you can do is just not bother with the include since you've already handled that by declaring it in your node definition. Also, because both are declared in your node definition you would want to order them there as well.

As a side note not providing default values is a bad practice as is using variables across modules.