On 2018-09-19 15:24, Albert Shih wrote:
Hi,

I'm would like to have a profile for example for apache.

   profile::apache

who can pass some hash to apache. For example let's say I've

   profile::apache::vhosts:
     vhost1:
     ....
     vhost2:
     ....

and I want to do

class profile::apache (
   Hash $vhosts
   )
{

   create_resources('apache::vhost', $vhosts)

}

But now I want to add some parameter who's not in the apache::vhost, for
example :

   profile::apache::vhosts:
     vhost1:
       - monitored : true
       ....
     vhost2:
       - monitored : false
       ....

so before I can do the

   create_resources('apache::vhost', $something)

i need to exclude « monitored » from that hash table. And...I don't know
how to do that. I try map, reduce etc.. and was unable to exclude some
nested key/value from a hash.

Regards


Puppet has a function named tree_each() that can be used to flatten and filter a tree structure of data. Once filtered it is possible to again create a Hash out of the result.

Documentation here: https://puppet.com/docs/puppet/5.5/function.html#treeeach

Here are two examples (both from the documentation; the first from tree_each(), and the second from Hash.new().

The first example shows the flattened filtered value.
To get the pruned hash in that example, do what is done in
Example 2 at the end - i.e. Hash($flattened_pruned_value, 'hash_tree').

It is really difficult to achieve the same with just reduce() and
filter() functions - you would have to more or less implement
the tree_each() function - but you don't have to since puppet has it :-)

Hope this helps you with what you were trying to do.

Also - note that it may be better for you (instead of filtering your values and then give the resulting structure to create_reources()), to
iterate over the structure and the simply have conditional logic
around the declaration of resources. That is much less magic to read.

Best
- henrik

Encourage you to play with these examples:

#### EXAMPLE 1
# A tree of some complexity (here very simple for readability)
$tree = [
 { name => 'user1', status => 'inactive', id => '10'},
 { name => 'user2', status => 'active', id => '20'}
]
notice $tree.tree_each.filter |$v| {
 $value = $v[1]
 $value =~ Hash and $value[status] == active
}


#### EXAMPLE 2
####
# A hash tree with 'water' at different locations
$h = { a => { b => { x => 'water'}}, b => { y => 'water'} }

# a helper function that turns water into wine
function make_wine($x) { if $x == 'water' { 'wine' } else { $x } }

# create a flattened tree with water turned into wine
$flat_tree = $h.tree_each.map |$entry| { [$entry[0], make_wine($entry[1])] }

# create a new Hash and log it
notice Hash($flat_tree, 'hash_tree')





--
Albert SHIH
DIO bâtiment 15
Observatoire de Paris
xmpp: [email protected]
Heure local/Local time:
Wed Sep 19 14:50:21 CEST 2018



--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

--
You received this message because you are subscribed to the Google Groups "Puppet 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/pntklh%2430b%241%40blaine.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to