简体   繁体   English

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

[英]How to filter hash in puppet manifest?

could you please help me. 请你帮助我好吗。 I'm getting dict from hiera in puppet manifest and then trying to filter them and pass in a python script as args. 我从人偶清单中的hiera获取dict,然后尝试过滤它们并将python脚本作为args传递。 But don't know how to do it. 但是不知道该怎么做。

My hiera: 我的等级:

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'

My puppet manifest(i'm getting hash from values in hiera): 我的人偶清单(我正在从hiera中的值获取哈希值):

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

As result of this i had: 结果,我有了:

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

Then i want to select from this list only values: 然后我只想从此列表中选择值:

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

I'm trying to do it using map function: 我正在尝试使用地图功能:

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

And getting error: 并得到错误:

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

How can i solve this problem? 我怎么解决这个问题? Also I need to transfer to one more variable 'testname2' 'testhost2' 'test@url.com2' and pass it to exec command resource. 另外,我还需要转移到另一个变量'testname2''testhost2''test@url.com2'并将其传递给exec命令资源。

Thank you! 谢谢!

It looks like there is a pretty good example of this on the Ask PuppetLabs forum: Iterate nested hash from hiera in manifest . 看起来在Ask PuppetLabs论坛上有一个很好的例子: 从manifest中的hiera迭代嵌套的哈希

The solution makes use of a defined type which runs your exec. 该解决方案利用运行您的exec的已定义类型 Then just iterate over your hash automatically with create_resources() , which converts a hash into a set of resources and adds them to the catalog. 然后,只需使用create_resources()自动遍历哈希即可 ,该哈希会将哈希转换为一组资源并将其添加到目录中。 This function makes it easy to create many resources from a Hiera data-source at once instead of having to write your own looping function. 使用此函数可以轻松地一次从Hiera数据源创建许多资源,而不必编写自己的循环函数。 It is best used with defined types, as they can be implemented many different times. 最好与定义的类型一起使用,因为它们可以在许多不同的时间实现。

I've adapted their example for your purposes: 我为您的目的改编了他们的示例:

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 )

Also, using an exec in puppet is a code smell. 另外,在人偶中使用exec是代码的味道。 Not that it's always bad, but often it's the least elegant way to solve the problem. 并非总是很糟糕,但是通常这是解决问题的最不优雅的方法。 For instance, is this exec configuring your server? 例如,这位高管正在配置您的服务器吗? If so, perhaps using a template to write the configuration file would be better. 如果是这样,也许使用模板来写配置文件会更好。 Here's another perspective on execs from the puppet docs for that type: 这是伪文件中针对该类型的exec的另一种观点:

A caution: There's a widespread tendency to use collections of execs to manage resources that aren't covered by an existing resource type. 警告:使用执行程序集合来管理现有资源类型未涵盖的资源的趋势已广泛存在。 This works fine for simple tasks, but once your exec pile gets complex enough that you really have to think to understand what's happening, you should consider developing a custom resource type instead, as it will be much more predictable and maintainable. 这对于简单的任务来说效果很好,但是一旦您的执行堆变得足够复杂,以至于您真的不得不考虑了解正在发生的事情,则应该考虑开发自定义资源类型,因为它将更加可预测和可维护。

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

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