What you're looking for is the dig() function.
https://puppet.com/docs/puppet/latest/function.html#dig

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

On Wed, Jul 11, 2018 at 8:32 AM Helmut Schneider <jumpe...@gmx.de> wrote:

> 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!
>
> --
> 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+unsubscr...@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.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CACkW_L40ZvgSM-AYJ7HBq_u%2B7MuBYL6GbfEE%3Di9qVn9f5Cu4rQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to