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



尝试使用此模块构建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,
}
}

这是我的主机1.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

代码中存在许多错误和误解。我修复了它们,以便代码至少可以编译并最终得到这个。

对配置文件的更改::绑定:

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

对配置文件::绑定::制作::区域更改:

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 中,我已经删除了您的密钥"区域"并在那里创建了一个嵌套的哈希。

必须包含绑定类

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

validate_hash功能是遗留的,在错误的地方

正如约翰·布林格(John Bollinger(在评论中提到的,你的validate_hash走错了线。我认为这是一个剪切/粘贴问题,因为如果这真的是您的代码,您将收到不同的错误消息。无论如何,由于您使用的是 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 中有拼写错误(-后没有空格(;其次,你传入了一个区域 NS 数组,然后试图将数组强制到你定义的类型的数组。

区域参数应为名称 var

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

重构为使用数据类型验证

请注意,我是如何对定义类型的输入使用 Puppet 的数据类型验证的,然后我就不再需要旧的validate_hash函数和其他相关的验证函数了。在此处阅读更多相关信息。

我认为仅此而已。希望对您有所帮助!

最新更新