Puppet 木偶嵌套资源创建_资源,可以';t将字符串转换为哈希
正在尝试使用此模块生成DNS:。但是得到这个错误:Puppet 木偶嵌套资源创建_资源,可以';t将字符串转换为哈希,puppet,Puppet,正在尝试使用此模块生成DNS:。但是得到这个错误: 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,但不确定它的格式是否正确,或者代码中是否存在其他问题。 这是我的d
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
代码中有许多错误和误解。我对它们进行了修复,这样代码至少可以编译并最终得到这样的结果 对概要文件的更改::绑定:
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”,并在那里创建了一个嵌套哈希
必须包括绑定类
camptocamp绑定模块要求也声明绑定类。见其文件
验证\u散列函数是遗留函数且位于错误位置
正如John Bollinger在评论中提到的,您的validate_hash
放错了行。我认为这是一个剪切/粘贴问题,因为如果这真的是您的代码,您会收到不同的错误消息。无论如何,因为您使用的是Puppet 5(我猜在您的Hiera中版本=>5),所以不要使用遗留的validate函数;使用Puppet的数据类型验证。所以我删除了那一行
使用lookup()而不是hiera\u hash()
同样,由于您使用的是Puppet 5,请使用lookup()
函数,而不是不推荐使用的hiera\u 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
区域类型混淆
您的区域有两个问题-首先,YAML中有一个输入错误(在-
之后没有空格);其次,传入一个区域NS的数组,然后尝试将该数组强制为定义类型的数组
区域参数应为名称var
注意,我必须删除定义类型中的$zone
参数,并使用特殊的$name
变量从标题中获取名称
重构以使用数据类型验证
请注意,我是如何对定义类型中的输入使用Puppet的数据类型验证的,然后我就不再需要遗留的validate\u hash
函数和其他相关的验证函数了。阅读更多关于这方面的信息
我想就这些。希望有帮助 在为$conf
赋值之前,classprofile::bind
调用validate\u hash($conf)
是非常可疑的。我无法想象这样做会产生令人满意的行为。谢谢你的回答!更干净的代码与您的答案!