Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In general, it's not possible to set variables outside of class definitions / defined types / functions in Puppet code. The site.pp file is special, and is the only place where that typically works.

Right now, your profiles::common::packages class file tries to set a variable outside of the class block.

$packages = lookup('packages::installed')
class profiles::common::packages {
   ...
}

What you should do:

Give your profile class a class parameter.

class profiles::common::packages (
  $packages,
) {
  package { $packages:
      ensure => installed,
  }
}

Then, make use of automatic class parameter lookups from Hiera to provide a value for it.

The short version is

  • For a class named profiles::common::packages
  • With a parameter named packages
  • Puppet will _automatically_ try to look up profiles::common::packages::packages in Hiera to populate that parameter

There are other ways to do this if you want. For example, you could do an explicit lookup if you put the lookup inside your class code. E.g.

class profiles::common::packages {
  $packages = lookup('packages::installed')
   ...
}

Check out this recommendation though on data escalation path for current best practices on when to use class parameters, and when to explicitly look up values. The most relevant paragraph from that doc is this one:

Parameterized profiles are preferred over explicit Hiera lookups. Class parameters make lookups consistent and present them all in a standardized way. Users can quickly determine the data API of a class without having to read or understand its logic, and class parameters also expose data lookup information about a class that can be useful for potential integration with external systems. Explicit Hiera data lookups (via calls to the lookup() or hiera() functions) dilute these benefits.

tl;dr class parameters are usually the way to go.