On 2018-11-06 17:13, Arnau wrote:
Hi all,
I'm using puppet 5.3.
I'd like to build proxy_pass array of hashes using the /reduce
/puppetbuilt-in function
<https://puppet.com/docs/puppet/5.5/function.html#reduce>and picking the
values from a nested hash in hiera.
I first played a little bit with the reduce function:
$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.
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.
Notice: MemoOut: [{path => /, url => http://localhost:189/}, {path =>
/static/, url => !}, {path => /media/, url => !}, {path => /photos/, url
=> !}]
Notice: /Stage[main]/Main/Node[default]/Notify[MemoOut: [{path => /, url
=> http://localhost:189/}, {path => /static/, url => !}, {path =>
/media/, url => !}, {path => /photos/, url => !}]]/message: defined
'message' as 'MemoOut: [{path => /, url => http://localhost:189/}, {path
=> /static/, url => !}, {path => /media/, url => !}, {path => /photos/,
url => !}]'
/To my eyes this is an array of hashes, and that's what I need to pass
to the apache vhost define./
So, now I want to pick the array (list of directories) from a nested
hash in hiera that look like:
application:
apache:
enable: true
servername: application
static_served:
- static
I have a define that picks the above hash.... The array with the list of
directories becomes "${apache['static_served']}" inside teh define.
I can print it using notice ${apache['static_served']}":
Notice: static_served: application [static]
This looks like an array but /*is_array */says that this is *not *an
array anymore.
And this is my first question, why it is not an array anymore?
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.
You probably wanted
$var = Array($apache['static_served'])
Which by the way is a no-op since $apache['static_served'] already is an
array (as per the hiera data you supplied).
$_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.
Hope this helps you get back on track.
- henrik
If I print the output (using notice $_proxy_pass and ) and I get a
weird output:
Notice:
/Stage[main]/.../Python::Virtualenv[application]/Notify[PROXY_PASS:
[{path => /, url => http://local
host:11080/}, {path => /[/, url => !}, {path => /s/, url => !}, {path =>
/t/, url => !}, {path => /a/, url => !}, {path => /t/, url => !}, {path
=> /i/, url => !},
{path => /c/, url => !}, {path => /]/, url => !}]]/message: defined
'message' as 'PROXY_PASS: [{path => /, url => http://localhost:11080/},
{path => /[/, url => !
}, {path => /s/, url => !}, {path => /t/, url => !}, {path => /a/, url
=> !}, {path => /t/, url => !}, {path => /i/, url => !}, {path => /c/,
url => !}, {path => /
]/, url => !}]'
reduce is taking all letters from "static" instead of 'static" as the
first (an uniqe) element from the array....
Later, in my code, I configure the proxy_pass in the apache:vhost like:
apache::vhost { "${apache['servername']}":
[...]
proxy_pass => "$_proxy_pass",
[...]
But, in the apache file, the proxy pass parameters looks like
ProxyPass path url
ProxyPassReverse path url
ProxyPass path url
ProxyPassReverse path url
[...]
So it's picking the keys from the hashes in the above array of hashes
and not the values.
I don't understand what is going on. Any help will be appreciated.
Best,
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]
<mailto:[email protected]>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-users/CAM69jx-wKXmP%3DhfPLsMsrYDeOS7TCTQaQ9XyhUaoWJ9uq%3DeZEg%40mail.gmail.com
<https://groups.google.com/d/msgid/puppet-users/CAM69jx-wKXmP%3DhfPLsMsrYDeOS7TCTQaQ9XyhUaoWJ9uq%3DeZEg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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/prsov3%24lfp%241%40blaine.gmane.org.
For more options, visit https://groups.google.com/d/optout.