[英]Complex structure with default values in Puppet
嘗試在 Puppet 中的哈希數組中設置默認值。
我正在嘗試配置要監視的 url。
它應該包含:
我得到以下代碼:
puppet apply test.pp
測試.pp:
class http_monitoring (
Array[Struct[{
url => String,
name => String,
runbook => String,
Optional[comment] => String,
Optional[secure] => Boolean,
Optional[warning] => Integer,
Optional[critical] => Integer,
}]]
$checks = [{
secure => true,
warning => 30,
critical => 15,
}]
){
$checks.each | Hash $check | {
notify {"${check}":}
}
}
class website {
class { 'http_monitoring':
checks => [
{url => 'https://example.com', name => 'example' ,runbook => 'https://link-to-docs.com/'},
{url => 'https://example2.com', name => 'example2' ,runbook => 'https://link-to-docs.com/', warning => 5, critical => 10},
]
}
}
include website
結果是:
Notice: {url => https://example.com, name => example, runbook => https://link-to-docs.com/}
Notice: /Stage[main]/Http_monitoring/Notify[{url => https://example.com, name => example, runbook => https://link-to-docs.com/}]/message: defined 'message' as '{url => https://example.com, name => example, runbook => https://link-to-docs.com/}'
Notice: {url => https://example2.com, name => example2, runbook => https://link-to-docs.com/, warning => 5, critical => 10}
Notice: /Stage[main]/Http_monitoring/Notify[{url => https://example2.com, name => example2, runbook => https://link-to-docs.com/, warning => 5, critical => 10}]/message: defined 'message' as '{url => https://example2.com, name => example2, runbook => https://link-to-docs.com/, warning => 5, critical => 10}'
Notice: Applied catalog in 0.01 seconds
我希望得到的輸出:
{url => 'https://example.com', name => example, runbook => https://link-to-docs.com/, secure => true, warning => 30, critical => 15}
{url => 'https://example2.com', name => 'example2', runbook => 'https://link-to-docs.com/', warning => 5, critical => 10, secure => true}
運行木偶 6.12.0
嘗試這個:
class http_monitoring (
Array[Struct[{
url => String,
name => String,
runbook => String,
Optional[comment] => String,
Optional[secure] => Boolean,
Optional[warning] => Integer,
Optional[critical] => Integer,
}]]
$checks = [{
secure => true,
warning => 30,
critical => 15,
}]
){
$check_defaults = {
secure => true,
warning => 30,
critical => 15,
}
# Checks with defaults
$checks_with_defaults_added = $checks.map | Hash $next_check | {
$check_defaults + $next_check
}
# $checks.each | Hash $check | {
# notify {"${check}":}
# }
$checks_with_defaults_added.each | Hash $check | {
$check_output = String($check)
notify { $check_output: }
}
}
class website {
class { 'http_monitoring':
checks => [
{url => 'https://example.com', name => 'example' ,runbook => 'https://link-to-docs.com/'},
{url => 'https://example2.com', name => 'example2' ,runbook => 'https://link-to-docs.com/', warning => 5, critical => 10},
]
}
}
include website
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.