0

I would like to create install_package module which can be called multi times, where packages are defined in hiera. I know class is skelton (can be called one time), and define type is designed for my purpose.

I have developped the following : hiera.yaml

---
version: 5
hierarchy:
  - name: "Common hierarchy level"
    path: "common.yaml"

./data/common.yaml

---
install_package::packages:
  - vim
  - logrotate

classes:
  - install_package

./module/install_package/manifest/init.pp

class install_package(
  packages=[],
) {

  define def_package()
    notify{"package to install is $name":}
  }

  def_package{ [$packages]: }
}

Puppet returns "Error while evaluating a Resource Statement, Unknown resource type: 'def_package'".

My question is how can I define packages to install in variable (array) in /data/common.yaml for instance, and then call install_package module multiple times, with define type ?

Colas06
  • 1
  • 1

1 Answers1

1

The error you are seeing is a scope problem. The def_package defined type has been defined within the install_package class, so its full name is actually install_package::def_package. In Puppet, classes and defined types have to be declared using their full name.

This works:

$ cat hiera.yaml
---
version: 5
hierarchy:
  - name: "Common hierarchy level"
    path: "common.yaml"
$ cat data/common.yaml
---
install_package::packages:
  - vim
  - logrotate

classes:
  - install_package
$ cat modules/install_package/manifests/init.pp
class install_package (
  $packages = [],
) {
  define def_package() {
    notice("package to install is ${name}")
  }

  install_package::def_package { $packages: }
}
$ puppet apply --hiera_config hiera.yaml -e "hiera_include('classes')" --modulepath modules
Notice: Scope(Install_package::Def_package[vim]): package to install is vim
Notice: Scope(Install_package::Def_package[logrotate]): package to install is logrotate
Notice: Compiled catalog for it070137.bris.ac.uk in environment production in 0.06 seconds
Notice: Applied catalog in 0.03 seconds

I'd note that defining classes and defined types within a class is not good style and makes puppet-lint complain.

Also, Puppet manifests support loops these days, so there is no need to use defined types with arrays as a workaround. Try a .each loop:

$ cat modules/install_package/manifests/init.pp
class install_package (
  $packages = [],
) {
  $packages.each |$name| {
    notice("package to install is ${name}")
  }
}
$ puppet apply --hiera_config hiera.yaml -e "hiera_include('classes')" --modulepath modules
Notice: Scope(Class[Install_package]): package to install is vim
Notice: Scope(Class[Install_package]): package to install is logrotate
Notice: Compiled catalog for it070137.bris.ac.uk in environment production in 0.05 seconds
Notice: Applied catalog in 0.03 seconds
Jon
  • 126
  • 4