繁体   English   中英

如何过滤人偶清单中的哈希?

[英]How to filter hash in puppet manifest?

请你帮助我好吗。 我从人偶清单中的hiera获取dict,然后尝试过滤它们并将python脚本作为args传递。 但是不知道该怎么做。

我的等级:

myclass::server_conf:
 'first_serv':
   'serv_name': 'testname'
   'serv_hostname': 'testhost'
   'test_url': 'test@url.com'
 'second_serv':
   'serv_name': 'testname2'
   'serv_hostname': 'testhost2'
   'test_url': 'test@url.com2'

我的人偶清单(我正在从hiera中的值获取哈希值):

 $server_conf = hiera_hash('myclass::server_conf', {})

结果,我有了:

{\"first_serv\"=>{\"serv_name\"=>\"testname\", \"serv_hostname\"=>\"testhost\", \"test_url\"=>\"test@url.com\"}, \"second_serv\"=>{\"serv_name\"=>\"serv2\", \"serv_name\"=>\"testname2\", \"serv_hostname\"=>\"testhost2\", \"test_url\"=>\"test@url.com2\"}}

然后我只想从此列表中选择值:

'testname' 'testhost' 'test@url.com' 'testname2' 'testhost2' 'test@url.com2'

我正在尝试使用地图功能:

$transforrmed_data = map(server_conf) |$key,$value| { $value }

并得到错误:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not match |$key,$value| at /manifests/server.pp:26 on node test.node

我怎么解决这个问题? 另外,我还需要转移到另一个变量'testname2''testhost2''test@url.com2'并将其传递给exec命令资源。

谢谢!

看起来在Ask PuppetLabs论坛上有一个很好的例子: 从manifest中的hiera迭代嵌套的哈希

该解决方案利用运行您的exec的已定义类型 然后,只需使用create_resources()自动遍历哈希即可 ,该哈希会将哈希转换为一组资源并将其添加到目录中。 使用此函数可以轻松地一次从Hiera数据源创建许多资源,而不必编写自己的循环函数。 最好与定义的类型一起使用,因为它们可以在许多不同的时间实现。

我为您的目的改编了他们的示例:

define run_my_exec($serv_name, $serv_hostname, $test_url) {
  notify { "$serv_name": }
}

$server_conf = hiera_hash('myclass::server_conf', {})
create_resources( run_my_exec, $server_conf )

另外,在人偶中使用exec是代码的味道。 并非总是很糟糕,但是通常这是解决问题的最不优雅的方法。 例如,这位高管正在配置您的服务器吗? 如果是这样,也许使用模板来写配置文件会更好。 这是伪文件中针对该类型的exec的另一种观点:

警告:使用执行程序集合来管理现有资源类型未涵盖的资源的趋势已广泛存在。 这对于简单的任务来说效果很好,但是一旦您的执行堆变得足够复杂,以至于您真的不得不考虑了解正在发生的事情,则应该考虑开发自定义资源类型,因为它将更加可预测和可维护。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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