【问题标题】:Iterate over a Puppet Hiera hash in a puppet manifest迭代 puppet manifest 中的 Puppet Hiera 哈希
【发布时间】:2019-07-19 11:40:36
【问题描述】:

更新 1

我稍微改变了 Hiera 数据的结构并尝试了不同的清单样式。


我正在尝试在 Puppet 清单中迭代以下 Hiera 哈希:

windows-10.yaml:

---
message: "This node is using Windows 10 data"
#profile::hiera_test::backups_enabled: false

profile::hiera_test::firewall:
  rule1:
    groupName: 'Cortana'
    profileNames: ['Private','Public']
    action: 'Deny'
  rule2:
    groupName: 'Microsoft Photos'
    profileNames: 'Public'
    action: 'Deny'

虽然我已经更新了数据结构并且puppet lookup... 返回了似乎有效的内容,但我对这个结构并不完全有信心。

我尝试了清单的多种排列方式。最新的如下所示(基于 Matt Schuchard 给出的this 回答):

hiera_test.pp:

class profile::hiera_test (
    Hash $data = lookup('profile::hiera_test::firewall', "merge" => 'hash'),
){
  $data.each | String $key, Hash $value = {}|{
    notify {
      default:
        name    => "Demo_${key}",
        message => 'Item DEFAULT',
      ;
      $key:
        * => $value,
    }
  }
}

以及上面的错误/输出:

PS C:\Users\LocalAdmin> puppet agent -t
Notice: Local environment: 'production' doesn't match server specified node environment 'development', switching agent to 'development'.
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Retrieving locales
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: no parameter named 'groupName' (file: /etc/puppetlabs/code/environments/development/site-modules/profile/manifests/hiera_test.pp, line: 31) on Notify[rule1] (file: /etc/puppetlabs/code/environments/development/site-modules/profile/manifests/hiera_test.pp, line: 31) on node winnode1.domain.com
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

理想情况下,我希望它在类声明中工作(为什么?因为这大约是我的 Puppet 学习所能达到的程度,但很高兴进一步学习。我也在使用 Puppet Enterprise (2019.0.2)) .

互联网上有几个类似的问题,但它们要么已经过时(Hiera this,要么我不知道如何将它们转换成我需要的。显然create_resources 是由于折旧 ?

如果有人能告诉我哪里出错了,那就太好了。

【问题讨论】:

  • 我们可以自己测试您使用代码获得的结果并猜测您想要达到的目标,但您可以分享这两个结果,以便我们在这里提供最好的帮助吗?

标签: puppet puppet-enterprise


【解决方案1】:

我有一个可行的解决方案,但希望有人能提出更好的方法。

对于问题中的示例,* => $value 行将 * 替换为来自 Hiera 哈希的键。这些键用作notify 资源的参数,根据错误,notify 没有这样的参数groupName。我要么必须更改我的 Hiera 数据中的键以匹配 notify 的参数,这没有多大意义,要么学习如何使用 Hiera 数据键作为参数...

反正Hiera的数据和问题里的一样,但是类如下:

class profile::hiera_test (
    Hash $data = lookup('profile::hiera_test::firewall', "merge" => 'hash'),
){
  $data.each | String $key, Hash $value = {}|{
    notify {
      default:
        name    => "Demo_${key}",
        message => 'Item DEFAULT',
      ;
      $key:
        name    => "Demo_${key}",
        message => "Output: GroupName: ${value['groupName']}, Profile Names: ${value['profileNames']}, Action: ${value['action']}",
    }
  }
}

如您所见,我已将* 替换为notify 资源的有效参数。

还有输出:

Notice: Output: GroupName: Cortana, Profile Names: [Private, Public], Action: Deny
Notice: Output: GroupName: Microsoft Photos, Profile Names: Public, Action: Deny

现在用真正的exec 资源替换Notify。毫无疑问,将profileNames 数组转换为 PowerShell 会很有趣。

【讨论】:

  • 它看起来很复杂,但仍不清楚您实际上要做什么。您只是想要一种避免使用create_resources() 的方法吗?
  • @AlexHarvey,我正在尝试遍历 Hiera 哈希。我不介意使用create_resources() 或任何其他方式。越简单越好(亲吻)。尝试了很多方法,但我的答案是我可以让它工作的唯一方法。我找不到create_resources() 的完整示例并阅读它将被弃用。谢谢。
猜你喜欢
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多