![](/img/trans.png)
[英]Lookup same keys in multiple hiera files in puppet using lookup command
[英]Passing elements in an array to hiera lookup as keys
嘗試執行人偶模塊時出現錯誤:
Error 400 on SERVER: certificatefqdn is not a hash or array when accessing it with certificatefile
這是我的hiera
文件的樣子:
testmodule::install::certificates:
test1.domain.com:
certificatefile: 'certificate1.crt'
certificatepass: 'testpass1'
test2.domain.com:
certificatefile: 'certificate2.crt'
certificatepass: 'testpass2'
test3.domain.com:
certificatefile: 'certificate3.crt'
certificatepass: 'testpass3'
我模塊中的init.pp
看起來像這樣:
class testmodule (
$certificates = hiera('testmodule::install::certificates'),
)
{
$domains = [
test1.domain.com',
test2.domain.com',
test3.domain.com',
[
testmodule::install { $domains:
certificates => $certificates,
}
}
我模塊中的install.pp
看起來像這樣:
define testmodule::install ($certificates)
{
$domain = $name
$certificatefqdn = $certificates["$domain"]
$certificatefile = $certificatefqdn['certificatefile']
$certificatepass = $certificatefqdn['certificatepass']
notify{"This is the certificate file: $certificatefile" :}
}
我期望為域數組中的每個元素看到這樣的輸出:
Notice: This is the certificate file: certificate2.crt
Notice: /Stage[main]/Testmodule/Testmodule::Install[certificate2.crt]/Notify[This is the certificate file: certificate2.crt]/message: defined 'message' as 'This is the certificate file: certificate2.crt'
相反,我看到了這個:
Notice: This is the certificate file: test2.domain.com['certificatefile']
Notice: /Stage[main]/Testmodule/Testmodule::Install[test2.domain.com]/Notify[This is the certificate file: test2.domain.com['certificatefile']]/message: defined 'message' as 'This is the certificate file: test2.domain.com['certificatefile']'
如何使用域中的元素作為初始密鑰正確訪問hiera中嵌套哈希中的密鑰?
該代碼似乎已經正確。
我已經根據您的問題進行了設置:
1 /
# manifests/init.pp
class test (
$certificates = hiera('test::install::certificates'),
) {
$domains = [
'test1.domain.com',
'test2.domain.com',
'test3.domain.com',
]
test::install { $domains:
certificates => $certificates,
}
}
2 /
# manifests/install.pp
define test::install ($certificates) {
$domain = $name
$certificatefqdn = $certificates["$domain"]
$certificatefile = $certificatefqdn['certificatefile']
$certificatepass = $certificatefqdn['certificatepass']
notify{"This is the certificate file: $certificatefile" :}
}
3 /
# spec/fixtures/hiera/data/common.yaml
---
test::install::certificates:
test1.domain.com:
certificatefile: 'certificate1.crt'
certificatepass: 'testpass1'
test2.domain.com:
certificatefile: 'certificate2.crt'
certificatepass: 'testpass2'
test3.domain.com:
certificatefile: 'certificate3.crt'
certificatepass: 'testpass3'
4 /
# spec/fixtures/hiera/hiera.yaml
---
version: 5
defaults:
datadir: data
data_hash: yaml_data
hierarchy:
- name: "All levels"
paths:
- common.yaml
編譯並應用:
$ bundle exec puppet apply --modulepath spec/fixtures/modules --hiera_config spec/fixtures/hiera/hiera.yaml -e 'include test'
Warning: The function 'hiera' is deprecated in favor of using 'lookup'. See https://docs.puppet.com/puppet/5.3/reference/deprecated_language.html
(file & line not available)
Notice: Compiled catalog for alexs-macbook-pro.local in environment production in 0.14 seconds
Notice: This is the certificate file: certificate1.crt
Notice: /Stage[main]/Test/Test::Install[test1.domain.com]/Notify[This is the certificate file: certificate1.crt]/message: defined 'message' as 'This is the certificate file: certificate1.crt'
Notice: This is the certificate file: certificate2.crt
Notice: /Stage[main]/Test/Test::Install[test2.domain.com]/Notify[This is the certificate file: certificate2.crt]/message: defined 'message' as 'This is the certificate file: certificate2.crt'
Notice: This is the certificate file: certificate3.crt
Notice: /Stage[main]/Test/Test::Install[test3.domain.com]/Notify[This is the certificate file: certificate3.crt]/message: defined 'message' as 'This is the certificate file: certificate3.crt'
Notice: Applied catalog in 0.04 seconds
所以我無法重現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.