epp: has_key

45 views
Skip to first unread message

Helmut Schneider

unread,
Jul 19, 2018, 11:17:45 AM7/19/18
to puppet...@googlegroups.com
Hi,

---
profiles:
vpn:
openvpn:
defaults:
client:
dev: 'tun'
proto: 'udp'
resolv-retry: 'infinite'
nobind:
user: 'nobody'
group: 'nogroup'
persist-key:
persist-tun:
ca: '/etc/openvpn/ca-charlieroot.de.crt'
cert: '/etc/openvpn/mail.helmut-ritter.de.crt'
key: '/etc/openvpn/mail.helmut-ritter.de.key'
ns-cert-type: 'server'
# OpenVPN <= 2.3
comp-lzo: 'yes'
# OpenVPN > 2.3
# compress: 'lz4'
log-append: '/var/log/openvpn.log'
verb: '4'
script-security: '2'
up: '/etc/openvpn/update-resolv-conf'

The eep is called by

content => epp("openvpn/etc/openvpn/config.epp", { openvpnConf =>
$openvpnConf, openvpnMode => $openvpnMode, instance => $instance }),

The epp:

<%- | Hash $openvpnConf, String $openvpnMode, String $instance | -%>
<% ({
'Mode' => [
"$openvpnMode",
],
'Connection' => [
'remote',
'proto',
'dev',
'persist-tun',
'nobind',
'resolv-retry',
'compress',
'comp-lzo',
],
'Privileges' => [
'user',
'group',
'persist-key',
],
'Authentication' => [
'cert',
'key',
'ca',
'ns-cert-type',
],
'Logging' => [
'verb',
'log-append',
],
'Misc' => [
'script-security',
'plugin',
'up',
'down',
],
}).each |$category, $parameters| { -%>
### <%= $category %> ###
<%#= $openvpnConf %>
<% $parameters.each |$parameter| { -%>
<% if $parameter == 'remote' { -%>
<%= $parameter %> <%= $openvpnConf['server'] %> <%=
$openvpnConf['port'] %>
<% } elsif $openvpnConf[$parameter] { -%>
<%= $parameter %> <%= regsubst($openvpnConf[$parameter],
'__INSTANCE__', $instance) %>
<% } elsif defined($openvpnConf[$parameter]) { -%>
<%= $parameter %>
<% } -%>
<% } %>
<% } -%>

The problem: compress does not exist in the hiera but is printed in the
epp. How can I test if the key "compress" exists in the
$openvpnConf-Hash?

Thank you.

Henrik Lindberg

unread,
Jul 19, 2018, 2:19:47 PM7/19/18
to puppet...@googlegroups.com
You can simply check if the hash has an Undef value for that key.

$openvpnConf['compress'] == undef

The stdlib module has a function named has_key() that you can use which
checks if the key is present and returns true even if value is set to
undef. If it really does not matter to you if key is set to undef or
simply missing it is enough to check if you get an undef value back.

If you are asking how you lookup a key to see if it is in hiera - call
the lookup() function and use an undef default - then check if value was
undef.

Hope that helps.
- henrik

> Thank you.
>


--

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

R.I. Pienaar

unread,
Jul 19, 2018, 2:43:00 PM7/19/18
to puppet...@googlegroups.com
Another option:

If “compress” in $openvpnConf { }


>
> The stdlib module has a function named has_key() that you can use which
> checks if the key is present and returns true even if value is set to undef. If it really does not matter to you if key is set to undef or simply missing it is enough to check if you get an undef value back.
>
> If you are asking how you lookup a key to see if it is in hiera - call
> the lookup() function and use an undef default - then check if value was undef.
>
> Hope that helps.
> - henrik
>
>> Thank you.
>
>
> --
>
> 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 puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/piqkjf%24si1%241%40blaine.gmane.org.
> For more options, visit https://groups.google.com/d/optout.

Helmut Schneider

unread,
Jul 20, 2018, 4:26:19 AM7/20/18
to puppet...@googlegroups.com
Did not work as I also want to print keys without values.

> Another option:
>
> If “compress” in $openvpnConf { }

Works for me, thank you!

Reply all
Reply to author
Forward
0 new messages