Ask Your Question
1

Hiera lookup question

asked 2015-03-28 01:26:45 -0600

sheshi gravatar image

updated 2015-03-29 00:51:23 -0600

csharpsteen gravatar image

Hi All,

If I do a lookup of a key in Hiera inside a Puppet manifest and if the key is not found, shouldn't hiera return 'nil' as opposed to throwing an error?

# puppet apply -e "notice(hiera('foo'))"
Error: Could not find data item foo in any Hiera data file and no default supplied on node ...
Error: Could not find data item foo in any Hiera data file and no default supplied on node ....

#hiera foo
nil

I need to configure some optional data in Hiera and use it in the manifest only if it is set, something like below

$foo_value = hiera('foo')
if $foo_value {
  # do something
}

How do I implement this? Any help is greatly appreciated.

Thanks

-Sheshi

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2015-03-28 03:14:19 -0600

updated 2015-03-28 03:14:41 -0600

You can provide a second argument to all hiera calls that will be the value returned is the item is not found:

puppet apply -e "notice(hiera('foo', <defaultvalue>))" eg:

puppet apply -e "notice(hiera('foo', undef))"
puppet apply -e "notice(hiera('foo', {}))"
edit flag offensive delete link more
0

answered 2015-03-28 09:18:08 -0600

sheshi gravatar image

updated 2015-04-02 17:06:00 -0600

Hi Greg,

Here is the YAML content.

deploy.yaml


base_location: /opt/base

foohome: location: %{hiera('baselocation')}/foo

boohome: location: %{hiera('baselocation')}/boo

I have other optional components that I can deploy (goo_home)

In the manifest file, I am doing

$foovalue = hiera('foohome') $foolocation = $foovalue['location']

And if I do a lookup for a non existent entry in the YAML, for example:

$goovalue = hiera('goohome')

I was getting "Error: Could not find data item foo in any Hiera data file and no default supplied on node ..."

so I changed it to

$goovalue = hiera('goohome', undef) if $goovalue { $goolocation = $goo_value['location'] }

That's when I got the error "Error: Evaluation Error: The value 'location' cannot be converted to a Numeric. on node "

What I didn't get was why is the if condition getting satisfied for 'undef' value? Once I changed the default to {}

$goovalue = hiera('goohome', {}) if $goovalue { $goolocation = $goo_value['location'] }

The error went away...

Thanks

-Sheshi

edit flag offensive delete link more
0

answered 2015-03-28 08:52:54 -0600

sheshi gravatar image

Thanks for your reply.

In-fact, I did just that. I was giving 'undef' as the default value. With 'undef' as the default, I was running into a different problem. In my case the optional data in Hiera is a block of key value pairs. I have the code as follows:

$foovalue = hiera('foo', undef) if $foovalue { $foo_location = $foovalue['location'] }

On applying that code, I got the following error,

Error: Evaluation Error: The value 'location' cannot be converted to a Numeric. on node ...

After changing the code to

$foovalue = hiera('foo', {}), it works. It appears I need to give empty {} as opposed to undef.

-Sheshi

edit flag offensive delete link more

Comments

Please post your Hiera data file(s). It appears that your YAML contains an array of values instead of a hash of values.

GregLarkin gravatar imageGregLarkin ( 2015-04-02 13:47:23 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-03-28 01:26:45 -0600

Seen: 1,760 times

Last updated: Apr 02 '15