Puppet 嵌套资源 create_resources,无法将字符串转换为哈希

Puppet nested resources create_resources, can't convert string into hash

尝试使用此模块构建 DNS:ref。但是出现此错误:

Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, can't convert String into Hash.

我嵌套了 YAML,但不确定它的格式是否正确,或者我的代码中是否存在其他问题。 这是我的 DNS 配置文件 dns.pp:

class profile::bind {

  validate_hash($conf)
  $conf   = hiera_hash('bind::zone', undef)
  create_resources('profile::bind::make::zone', $conf)

}

这就是我用 make_zone.pp:

定义我的区域的方式
define profile::bind::make::zone (
  $hash_data,
  $zone,
  $ensure,
  $zone_contact,
  $zone_ns,
  $zone_serial,
  $zone_ttl,
  $zone_origin,
) {

  validate_hash($hash_data)

  bind::zone { $zone :
    ensure       => $ensure,
    zone_contact => $zone_contact,
    zone_ns      => [$zone_ns],
    zone_serial  => $zone_serial,
    zone_ttl     => $zone_ttl,
    zone_origin  => $zone_origin,
  }
}

这是我的 host1.yaml 数据:

---
version: 5

bind::zone:
  zone: test.ltd
  ensure: present
  zone_contact: 'contact.test.ltd'
  zone_ns: 
    -'ns0.test.ltd'
    -'ns1.test.ltd'
  zone_serial: '2018010101'
  zone_ttl: '767200'
  zone_origin: 'test.ltd'
  hash_data:
    "newyork":
      owner: "11.22.33.44"
    "tokyo":
      owner: "22.33.44.55"
    "london":
      owner: "33.44.55.66"

bind::cname:
  ensure: present
  record_type: master

代码中存在一些错误和误解。我修复了它们,这样代码至少可以编译并以这个结尾。

更改为 profile::bind:

class profile::bind {
  include bind
  $conf = lookup('bind::zone')
  create_resources(profile::bind::make::zone, $conf)
}

对 profile::bind::make::zone:

的更改
define profile::bind::make::zone (
  Enum['present','absent'] $ensure,
  String         $zone_contact,
  Array[String]  $zone_ns, 
  String         $zone_serial,
  String         $zone_ttl,
  String         $zone_origin,
  Hash[String, Hash[String, String]] $hash_data,
) {
  bind::zone { $name:
    ensure       => $ensure,
    zone_contact => $zone_contact,
    zone_ns      => $zone_ns,
    zone_serial  => $zone_serial,
    zone_ttl     => $zone_ttl,
    zone_origin  => $zone_origin,
  }
}

对 host1.yaml 的更改:

---
bind::zone:
  'test.ltd':
    ensure: present
    zone_contact: 'contact.test.ltd'
    zone_ns:
      - 'ns0.test.ltd'
      - 'ns1.test.ltd'
    zone_serial: '2018010101'
    zone_ttl: '767200'
    zone_origin: 'test.ltd'
    hash_data:
      "newyork":
        owner: "11.22.33.44"
      "tokyo":
        owner: "22.33.44.55"
      "london":
        owner: "33.44.55.66"

一些解释:

直接问题:

Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, can't convert String into Hash.

此错误是由于您的 Hiera 数据结构不正确 Hash[String, Hash[String, String]]。请注意,在 yaml 中我删除了您的密钥 "zone" 并在那里创建了一个嵌套哈希。

必须包含绑定 class

camptocamp BIND 模块还需要声明绑定 class。请参阅其文档。

validate_hash 函数是遗留的并且在错误的地方

正如 John Bollinger 在评论中提到的,您在错误的行上输入了 validate_hash。我认为这是一个 cut/paste 问题,因为如果那确实是您的代码,您会收到不同的错误消息。无论如何,由于您使用的是 Puppet 5(我猜您的 Hiera 中的版本 => 5),请不要使用旧版验证功能;使用 Puppet 的数据类型验证。所以我刚刚删除了那一行。

使用 lookup() 而不是 hiera_hash()

同样,由于您使用的是 Puppet 5,请使用 lookup() 函数而不是已弃用的 hiera_hash() 函数。

版本 5 属于 hiera.yaml,而不是 host1.yaml

它不会给您带来任何问题,但是 version: 5 行不会在这里执行任何操作,它属于您的 hiera.yaml 文件。我使用如下 hiera.yaml 文件进行测试:

---
version: 5
defaults:
  datadir: data
  data_hash: yaml_data
hierarchy:
  - name: "Host 1"
    paths:
    - host1.yaml

zone_ns类型混淆

您在 zone_ns 方面遇到了 2 个问题 - 首先,您的 YAML 中有错别字(- 之后没有 space);其次,您传入了一个区域 NS 数组,然后尝试将该数组强制转换为您定义类型的数组。

zone参数应该是名字var

请注意,我必须删除您定义的类型中的 $zone 参数,并且我使用了特殊的 $name 变量来从标题中获取名称。

重构为使用数据类型验证

请注意我是如何在定义的类型中对您的输入使用 Puppet 的数据类型验证的,然后我就不再需要遗留的 validate_hash 函数和其他相关的验证函数了。详细了解 here

我想就这些了。希望对您有所帮助!