简体   繁体   中英

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. 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):

 $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.

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 .

The solution makes use of a defined type which runs your 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. 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. 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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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