通过 Hiera 将包含包数据的 yaml 数组传递给 puppet

Passing a yaml array containing package data to puppet through Hiera

人偶版本:4.9.4 hiera 版本:3.3.1

我想做的是在安装 package-x/y 的新版本时重新加载 httpd,但似乎没有正确传递来自 Hiera 的数组。

对于我的 httpd.pp 文件,我有:

class service::common::httpd (
  $service_state = undef, # undef = unmanaged
  $service_run_at_boot = undef,
  $packages = undef
  ) {
    service { 'httpd':
      ensure     => $service_state,
      enable     => $service_run_at_boot,
      subscribe  => $packages,
      restart    => "/usr/sbin/apachectl graceful"
    }
  }

在 hiera 的 yaml 文件中,我有:

service::common::httpd::packages: [Package['package-x'],Package['package-y']]

运行 puppet 给出错误

Error: Evaluation Error: Error while evaluating a Function Call, Lookup of key 'allow_virtual_packages' failed: Unable to parse (/root/repos/puppet-config/data/nodes/<location of yaml file>): did not find expected ',' or ']' while parsing a flow sequence

也说是missing a comma between flow collection entries。我也尝试了许多不同的空格和逗号组合..

我也尝试过使用 include 语句将包包含在 class 中。

我做错了什么?

yamllint 实用程序对于分析 Puppet Hiera YAML 文件非常有用。当我在你的文件上尝试时,我得到:

▶ yamllint spec/fixtures/hiera/data/common.yaml 
spec/fixtures/hiera/data/common.yaml
  2:25      error    syntax error: expected ',' or ']', but got '['
  2:39      error    too few spaces after comma  (commas)

那里的语法错误表明该文件只是无效的 YAML。

但是如何解决呢?

令人困惑的是,Puppet 清单中的一行如下:

  subscribe => [Package['package-x'], Package['package-y']]

当编译成JSON Puppet 目录时变成:

  "subscribe": ["Package[package-x]", "Package[package-y]"]

并且您可以在 YAML 文件中放置相同的 JSON 字符串以生成有效的 YAML,如下所示:

service::common::httpd::packages: ["Package[package-x]", "Package[package-y]"]

您还可以在 YAML 中使用单引号,即

service::common::httpd::packages: ['Package[package-x]', 'Package[package-y]']

有关如何在我的博客中编译 Puppet 目录的更多信息 post here

引用。

hiera 不知道 Package 是什么。只是引用它,因为它是一个字符串。

service::common::httpd::packages: [ "Package['package-x']", "Package['package-y']" ]

完美运行。


或者您可以将 [ "Package['package-x']", "Package['package-y']" ] 更改为 [ 'package-x', 'package-y' ]

完美运行。见下文。

host01.yaml

beats::packetbeat::packages: [ acl, htop ]

packetbeat.pp

class beats::packetbeat (
    $packages = undef
) {
    package {
        "packetbeat":
            ensure    => "$version",
            subscribe => Package[$packages],
        ;
    } 
}

Notice: /Stage[main]/Beats::Packetbeat/Package[acl]/ensure: current_value 'absent', should be '2.2.51-14.el7' (noop)

Notice: /Stage[main]/Beats::Packetbeat/Package[packetbeat]: Would have triggered 'refresh' from 1 event

Notice: /Stage[main]/Beats::Packetbeat/Service[packetbeat]: Would have triggered 'refresh' from 2 events

Notice: Class[Beats::Packetbeat]: Would have triggered 'refresh' from 3 events