简体   繁体   English

Puppet 中具有默认值的复杂结构

[英]Complex structure with default values in Puppet

Trying setup default values in an Array of Hashes in Puppet.尝试在 Puppet 中的哈希数组中设置默认值。
I am trying to configure urls to monitor.我正在尝试配置要监视的 url。
It should contain:它应该包含:

  • url网址
  • name姓名
  • runbook操作手册
  • comment[Optional]评论[可选]
  • secure [default=true]安全 [默认=真]
  • warning [default=30]警告 [默认 = 30]
  • critical [default=15]关键 [默认 = 15]

I got the following code:我得到以下代码:

puppet apply test.pp

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

Results in:结果是:

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

The output I was hoping to get:我希望得到的输出:

{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}

Running Puppet 6.12.0运行木偶 6.12.0

Try this:尝试这个:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM