Puppet:从 hiera 调用类中的定义类型

Puppet:从 hiera 调用类中的定义类型

我想创建可以多次调用的 install_package 模块,其中包在层次结构中定义。我知道类是 skelton(可以调用一次),并且定义类型是为我的目的而设计的。

我已经开发了以下内容: hiera.yaml

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

./数据/common.yaml

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

classes:
  - install_package

./模块/安装包/manifest/init.pp

class install_package(
  packages=[],
) {

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

  def_package{ [$packages]: }
}

傀儡归来评估资源语句时出错,未知资源类型:“def_package”“。

我的问题是,如何定义要安装在 /data/common.yaml 中的变量(数组)中的包,然后使用定义类型多次调用 install_package 模块?

答案1

您看到的错误是范围问题。def_package定义的类型已在install_package类中定义,因此其全名实际上是install_package::def_package。在 Puppet 中,类和定义的类型必须使用其全名进行声明。

这有效:

$ 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

我注意到,定义类并在类中定义类型并不是好的风格并且会引起puppet-lint抱怨。

此外,Puppet 清单现在支持循环,因此无需使用数组作为解决方法的定义类型。尝试循环.each

$ 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

相关内容