Ask Your Question
0

hiera_hash not merging hash

asked 2016-09-26 14:54:19 -0500

mazdajai gravatar image

updated 2016-09-26 21:42:53 -0500

I have two set of hash in the module and in yaml.

I am expected to see values in the module would override the ones in common but it didn't happen and got undefined instead. Thoughts?

*Edited to post solution.

networker\init.pp

     $defaults = {
      pre  => '',
      post => '',
      save     => '',
    }
    $options  = merge( hiera_hash('networker::options'),$networker::defaults )
    validate_hash($networker::options)
$pre  = $networker::options[pre] #Edited 
$post = $networker::options[post] #Edited 
$save = $networker::options[save] #Edited

common.yaml

networker::options:
    pre             : 'foo'
    save            : 'bar'

foo.erb

Post : <%= scope.lookupvar('networker::post') %>;
Pre : <%= scope.lookupvar('networker::pre') %>;
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-09-26 17:07:58 -0500

lavaman gravatar image

You appear to be referencing undefined variables in your erb template. From your code, you'd need to use @options['pre'], @options['post'], etc instead of scope.lookupvar.

edit flag offensive delete link more

Comments

You nailed it, I forgot to put the hash value back to the variable. Thanks lavaman.

mazdajai gravatar imagemazdajai ( 2016-09-26 21:40:08 -0500 )edit
0

answered 2016-09-26 15:03:45 -0500

DarylW gravatar image

You are using the method backwards..

The first argument should be the hash you want to merge to, and the second argument should be the hash you want to merge from.

https://forge.puppet.com/puppetlabs/stdlib#merge

Merges two or more hashes together and returns the resulting hash.

Example:

$hash1 = {'one' => 1, 'two' => 2}
$hash2 = {'two' => 'dos', 'three' => 'tres'}
$merged_hash = merge($hash1, $hash2)
# The resulting hash is equivalent to:
# $merged_hash =  {'one' => 1, 'two' => 'dos', 'three' => 'tres'}



When there is a duplicate key, the key in the rightmost hash "wins." Type: rvalue.
edit flag offensive delete link more

Comments

I tried swapping the order in merge () but doesn't work - I expect to see the blank that I set in the modules but still getting undefined.

mazdajai gravatar imagemazdajai ( 2016-09-26 15:35:36 -0500 )edit

Ah, so you had two problems, your above problem was why you weren't seeing a value, and this problem would have been a logic error with your data!

DarylW gravatar imageDarylW ( 2016-09-27 08:20:52 -0500 )edit

Yes sir. I should have known when I see 'undefined' being returned, it was from ruby template. Interesting, documentation refer 'nil' from Ruby template and mention nothing about 'undefined'. https://docs.puppet.com/puppet/3/reference/lang_datatypes.html

mazdajai gravatar imagemazdajai ( 2016-09-27 16:42:27 -0500 )edit

I think it has more to do with the erb parsing... once I accidently commented out my code in an erb assignment <%= #'thing' %> and I believe I got a similar error. (you CAN comment out code in a regular block <% %>, just not in an assignment). I think there is some other checking that goes on

DarylW gravatar imageDarylW ( 2016-09-28 08:34:35 -0500 )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

Stats

Asked: 2016-09-26 14:54:19 -0500

Seen: 63 times

Last updated: Sep 26 '16