Hi Henrik,

first of all, thanks for your answer.

El mar., 6 nov. 2018 a las 20:12, Henrik Lindberg (<
[email protected]>) escribió:
[...]

> > $dirs = ['static','media','photos']
> > $proxy = $dirs.reduce([ { 'path' => '/', 'url' =>
> > "http://localhost:${port}/"; } ]) |$memo, $value| { $memo + [ { 'path'
> =>
> > "/$value/", 'url' => '!' } ] }
> > notify { "MemoOut: $proxy" : ;}
> >
> > If I puppet apply the above code the output looks like:
>
> Avoid having  notify with a title containing [ ] - it can get very
> confusing. Use a title like "testing", and set the message instead.
>

Ok, thanks. Then:
 notify {"test $name" :
    message => "static_served: ${apache[static_served]}",
 }
produces:
 defined 'message' as 'static_served: [static]'

*I need to enclose the hash in curly braces otherwise it shows the whole
hash like:*
defined 'message' as 'Message {enable => true, servername => application,
static_served => [static]}[static_served]'


You give the reduce an array with a hash in it, and you then append to
> that array with an array containing a hash. The result will be an
> array where each element is a hash.
>

Yes, exaclty. That's what I need (if I have not missunderstood it) to pass
tot he proxy_pass parameter in the apache forge module:

 proxy_pass => [
    { 'path' => '/a', 'url' => 'http://backend-a/' },
    { 'path' => '/b', 'url' => 'http://backend-b/' },
]

[...]

> > I make this an array using any2array (array un puppet 5) so I can still
> > call the reduce function::
> >
> > $var=(Array("${apache['static_served']}"))
> >
> > so the /reduce /line now looks like:
> >
> You are asking Array to create an array out of a string. It will
> construct that as an array where each element is a single character string.
>

Ah, ok, cause I'm passing the first element of the array and not the array
to the Array function? and this is becasue of the quoting, right?

You probably wanted
>
> $var = Array($apache['static_served'])
>


> > $_proxy_pass = $var.reduce([ { 'path' => '/', 'url' =>
> > "http://localhost:${port}/"; } ]) |$memo, $value| { $memo + [ { 'path'
> =>
> > "/$value/", 'url' => '!' } ] }
> >
> > And here is where everythiong starts to make even less sense:
> >
>
> You are now performing a reduce based on a sequence of single character
> strings. This is indeed confusing as you have already gone off the road.
>

So, at the end it was as simple as calling the reduce function like:
$apache['static_served'].reduce

(I as using a more larger hiera data and, in the very first application,
the static_serverd array was empty, so reduce was failling all the time
cause the array was empty... Damn!!

now the code looks like:

if $apache['static_served'] {
    $_proxy_pass =  $apache['static_served'].reduce([ { 'path' => '/', 'url'
=> "http://localhost:${port}/"; } ]) |$memo, $value| { $memo + [ { 'path' =>
"/$value/", 'url' => '!' } ] }
  }else{
   $_proxy_pass =  [ { 'path' => '/', 'url' => "http://localhost:${port}/";
} ]
  }
  notify { "PROXY: $name" :
    message => $_proxy_pass,
  }

But the reduce is now not working:

Notice: {"path"=>"/", "url"=>"http://localhost:56902/"}
Notify[PROXY: application]/message: defined 'message' as {
  'path' => '/',
  'url' => 'http://localhost:56902/'
}


what am I doing wrong now?



BTW, I find the hash notation/manipulation a little confusing... According
to https://puppet.com/docs/puppet/5.4/lang_data_hash.html it must be as
simple as:

$key[$value]

And quotes should be used in "keys" when they are strings, but in the above
example:

$var = Array($apache['static_served'])

 you are quoting the $value when it's a array and not a string...

what is the generic rule for quoting in hashes?


Hope this helps you get back on track.
>
> - henrik
>

Thanks again!
Arnau

-- 
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/CAM69jx9evODEdpT%2B-aCoHtf6iZc1j1pxguA7%2B6onaBAb31daBA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to