how do I check to see whether a hash value is defined if a parent of the hash value might not even exist?

158 views
Skip to first unread message

Steve Neuharth

unread,
Mar 10, 2014, 4:42:36 PM3/10/14
to puppet...@googlegroups.com
I've got a big hash of info that looks like this:

  $web_conf = {
    my_client => {
      dev => {
        ws => {
          server_name => 'ws.foo.bar',
          server_aliases => ['ws.dev.bar.foo.com', 'ws.dev.another.foo.com'],
        },
        csr => {
          server_aliases => ['csr.dev.bar.foo.com', 'csr.dev.another.foo.com'],
        },
        mw => {
          server_aliases => ['mw.dev.bar.foo.com', 'mw.dev.another.foo.com'],
        },
      },
    }

I'm trying to do something like this in my manifest:

  # set up our servername
  if $web_conf[$::client][$::env][$name]['server_name'] {
    $server_name = $web_conf[$::client][$::env][$name]['server_name']
  } else {
    $server_name = "${name}.${client_code}.${::env}.${domain}"
  }

The thought was that I'd have servers that follow a certain naming convention and ones that don't. The ones that have 'special' names go in the web_conf hash. If the manifest 'sees' a server name in the hash, it should use that. If not, then it just does the 'else' bit.

The trouble is that it totally fails if even the top or intermediate levels of the hash don't exist. I've tried using "if $web_conf[$::client][$::env][$name]['server_name'] != undef", tried "if undef($web_conf[$::client][$::env][$name]['server_name'])" etc.

what's the correct way to do this?
--steve 

Steve Neuharth

unread,
Mar 11, 2014, 11:34:16 AM3/11/14
to puppet...@googlegroups.com
this works:


  # set up our servername
  if $web_conf[$::client] {
    if $web_conf[$::client][$::env] {
      if $web_conf[$::client][$::env][$name] {

        if $web_conf[$::client][$::env][$name]['server_name'] {
          $server_name = $web_conf[$::client][$::env][$name]['server_name']
        } else {
          $server_name = "${name}.${client_code}.${::env}.${domain}"
        }
      } else {
        $server_name = "${name}.${client_code}.${::env}.${domain}"
      }
    } else {
      $server_name = "${name}.${client_code}.${::env}.${domain}"
    }
  } else {
    $server_name = "${name}.${client_code}.${::env}.${domain}"
  }

but it's SO UGLY. Any recommendations on making it prettier?

David Schmitt

unread,
Mar 11, 2014, 12:00:46 PM3/11/14
to puppet...@googlegroups.com
What about using hiera:

data.yaml:
---
client::dev::sw::server_name: ws.foo.bar

pp:
$server_name =
hiera("client::${::client}::${::env}::${name}::server_name", "default
value")

Regards, David

On 2014-03-11 16:34, Steve Neuharth wrote:
> this works:
>
> # set up our servername
> if $web_conf[$::client] {
> if $web_conf[$::client][$::env] {
> if $web_conf[$::client][$::env][$name] {
> if $web_conf[$::client][$::env][$name]['server_name'] {
> $server_name = $web_conf[$::client][$::env][$name]['server_name']
> } else {
> $server_name = "${name}.${client_code}.${::env}.${domain}"
> }
> } else {
> $server_name = "${name}.${client_code}.${::env}.${domain}"
> }
> } else {
> $server_name = "${name}.${client_code}.${::env}.${domain}"
> }
> } else {
> $server_name = "${name}.${client_code}.${::env}.${domain}"
> }
>
> but it's SO UGLY. Any recommendations on making it prettier?
>
> On Monday, March 10, 2014 3:42:36 PM UTC-5, Steve Neuharth wrote:
>
>> I've got a big hash of info that looks like this:
>>
>> $web_conf = {
>> my_client => {
>> dev => {
>> ws => {
>> server_name => 'ws.foo.bar',
>> server_aliases => ['ws.dev.bar.foo.com [1]',
>> 'ws.dev.another.foo.com [2]'],
>> },
>> csr => {
>> server_aliases => ['csr.dev.bar.foo.com [3]',
>> 'csr.dev.another.foo.com [4]'],
>> },
>> mw => {
>> server_aliases => ['mw.dev.bar.foo.com [5]',
>> 'mw.dev.another.foo.com [6]'],
>> },
>> },
>> }
>>
>> I'm trying to do something like this in my manifest:
>>
>> # set up our servername
>> if $web_conf[$::client][$::env][$name]['server_name'] {
>> $server_name = $web_conf[$::client][$::env][$name]['server_name']
>> } else {
>> $server_name = "${name}.${client_code}.${::env}.${domain}"
>> }
>>
>> The thought was that I'd have servers that follow a certain naming
>> convention and ones that don't. The ones that have 'special' names
>> go in the web_conf hash. If the manifest 'sees' a server name in the
>> hash, it should use that. If not, then it just does the 'else' bit.
>>
>> The trouble is that it totally fails if even the top or intermediate
>> levels of the hash don't exist. I've tried using "if
>> $web_conf[$::client][$::env][$name]['server_name'] != undef", tried
>> "if undef($web_conf[$::client][$::env][$name]['server_name'])" etc.
>>
>> what's the correct way to do this?
>> --steve
>
> --
> 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 [7].
> To view this discussion on the web visit
>
> https://groups.google.com/d/msgid/puppet-users/3355ea2c-0aae-436b-9098-d98a7de54dfb%40googlegroups.com
> [8].
> For more options, visit https://groups.google.com/d/optout [9].
>
>
> Links:
> ------
> [1]
>
> http://www.google.com/url?q75http%3A%2F%2Fws.dev.bar.foo.com46sa75D46sntz�75146usg75AFQjCNFmX6cyyTaccoxrdubRxxpiElZdiA
> [2]
>
> http://www.google.com/url?q75http%3A%2F%2Fws.dev.another.foo.com46sa75D46sntz�75146usg75AFQjCNH3NlITRPS_yRUnnGBU5opR17P7qw
> [3]
>
> http://www.google.com/url?q75http%3A%2F%2Fcsr.dev.bar.foo.com46sa75D46sntz�75146usg75AFQjCNFFDzU4C1eUsUjzElCeOoQ3wAfpDw
> [4]
>
> http://www.google.com/url?q75http%3A%2F%2Fcsr.dev.another.foo.com46sa75D46sntz�75146usg75AFQjCNGdGhCjs7JRYmkLXmT3Z194V2MkBw
> [5]
>
> http://www.google.com/url?q75http%3A%2F%2Fmw.dev.bar.foo.com46sa75D46sntz�75146usg75AFQjCNHrYg2mzHmEkTedOjOqvRgIOOiEdA
> [6]
>
> http://www.google.com/url?q75http%3A%2F%2Fmw.dev.another.foo.com46sa75D46sntz�75146usg75AFQjCNF0vYQdfXCDOt2znwABX_D1peMUvw
> [7] mailto:puppet-users...@googlegroups.com
> [8]
>
> https://groups.google.com/d/msgid/puppet-users/3355ea2c-0aae-436b-9098-d98a7de54dfb%40googlegroups.com?utm_medium=email&utm_source=footer
> [9] https://groups.google.com/d/optout

Steve Neuharth

unread,
Mar 11, 2014, 3:39:33 PM3/11/14
to puppet...@googlegroups.com
very interesting. Maybe I need to just bite the bullet and use hiera.

Felix Frank

unread,
Apr 16, 2014, 8:55:55 AM4/16/14
to puppet...@googlegroups.com
Hi,

the puppetlabs-stdlib module introduces a has_key() function to puppet.

HTH,
Felix
Reply all
Reply to author
Forward
0 new messages