Check existence of a hierarchy

11 views
Skip to first unread message

Helmut Schneider

unread,
Jul 11, 2018, 11:32:24 AM7/11/18
to puppet...@googlegroups.com
Hello all,

let's assume the following structure in a yaml:

profiles:
vpn:
openvpn:
instances:
client:
myclient:
remote: 'openvpn_host 1194'

I would now like to check if e.g. "client" exists:

if ($profiles['vpn']['openvpn']['instances']['client'])

This works as long as the structure

profiles:
vpn:
openvpn:
instances:

exists and fails if not (because OpenVPN should not be available for
that client):

Error: Could not retrieve catalog from remote server: Error 500 on
SERVER: Server Error: Evaluation Error: Operator '[]' is not applicable
to an Undef Value. at
/etc/puppetlabs/code/modules/openvpn/manifests/init.pp:17:7 on node
my_client

Is there a way to check the existence of a hierarchy without creating
an empty hierarchy or doing something like

if is_hash($profiles) {
if has_key($profiles, 'vpn') {
if has_key($profiles['vpn'], 'openvpn') {
if has_key($profiles['vpn']['openvpn'], 'instances') {
...and so on
}
}
}
}

Thank you!

Ben Ford

unread,
Jul 11, 2018, 11:40:20 AM7/11/18
to puppet...@googlegroups.com
What you're looking for is the dig() function. https://puppet.com/docs/puppet/latest/function.html#dig

$profiles.dig('vpn', 'openvpn', 'instances', 'client')

--
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/xn0lcc078szqyl1000%40news.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Helmut Schneider

unread,
Jul 11, 2018, 12:35:49 PM7/11/18
to puppet...@googlegroups.com
Great, thanks a lot!

Reply all
Reply to author
Forward
0 new messages