[英]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中,我刪除了您的鍵“區域”,並在其中創建了一個嵌套的哈希。
必須包含綁定類
camptocamp BIND模塊要求還聲明綁定類。 請參閱其文檔。
validate_hash函數是舊函數,並且放置在錯誤的位置
正如約翰·布林格(John Bollinger)在評論中提到的那樣,您在錯誤的行上輸入了validate_hash
。 我認為這是一個剪切/粘貼問題,因為如果這確實是您的代碼,那么您將收到一條不同的錯誤消息。 無論如何,由於您正在使用Puppet 5(我想您的Hiera中的版本=> 5),所以不要使用傳統的validate函數; 使用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
遇到了兩個問題-首先,您的YAML中有一個錯字( -
后面沒有空格); 其次,您傳入了一個區域NS的數組,然后嘗試將該數組強制為您定義類型的數組。
區域參數應為名稱var
注意,我必須刪除您定義的類型中的$zone
參數,而是使用特殊的$name
變量來從標題中獲取名稱。
重構以使用數據類型驗證
請注意,我是如何在定義類型的輸入上使用Puppet的數據類型驗證的,然后我就不再需要舊版validate_hash
函數和其他相關的validate函數。 在這里閱讀更多有關它的信息 。
我認為就這些。 希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.