Ask Your Question

update hash in puppet manifest

asked 2017-03-16 04:13:45 -0600

jondoe gravatar image


I am using a puppet agent 3.7.1 as part of our deployment process. We also use hiera to store our configuration data. Somewhere in one of my manifest I call hiera_hash to merge my configuration dictionary. My problem is that I need to "enrich" the returned hash and add some data to it. I understand that variables are immutable in puppet, so I can't just go

myhash[newkey] = new_value

What options do I have if I want to update my hash ?(stdlib is not an option for me...the redtape in the place I work makes it very hard to install anything that does not come out of the box)


edit retag flag offensive close merge delete


Where is this new data coming from / why do you need additional data? What are you doing with the data? These are important questions to answer in order to judge how new data can be incorporated and why it wasn't initially.

puser gravatar imagepuser ( 2017-03-16 06:26:06 -0600 )edit

i don't think it's relevant actually. The issue here is how can you update a hash(forget about hiera/ was just to give some context) Suppose I define a hash: $my_hash = {'k'=>'v'} Then for some reason I want to update it. Since it is immutable, I can't do $my_hash['new_key']='new_val'

jondoe gravatar imagejondoe ( 2017-03-16 07:13:51 -0600 )edit

-- continued So how would you go about updating a hash thusly defined.

jondoe gravatar imagejondoe ( 2017-03-16 07:14:33 -0600 )edit

I am wondering why the data isn't complete in hiera in the first place and where the extra data is coming from on the fly.

puser gravatar imagepuser ( 2017-03-16 07:15:46 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2017-03-16 07:30:52 -0600

DarylW gravatar image

I work in an environment where there is a huge process to bring in external code as well, but stdlib is well worth going through the red tape. You may be able to get any of the modules that are owned by puppetlabs in under the same banner as puppet. Infact, I have even seen an RPM version of stdlib in our EPEL repository that we have available. The only other module that I would highly recommend to prevent recreating it poorly is the puppet-archive module.

Directly answering your question. You cannot 'update' an existing hash, but you can merge two hashes into a new hash. This pattern also works for having default values. merge (and deep_merge) are a part of the stdlib, and can be used like this..

$h1 = { 'a' => '1' }
$h2 = { 'a' => '2', 'b' => '3' }
notice( merge($h1, $h2) ) # Notice: Scope(Class[main]): {"a"=>"2, "b"=>"3"}

This pattern can be used for merging in a passed in config to a default hash, as well as merging other fragments into an already existing hash. You just can't reuse the same variable. If you have an arbitrary number of elements to combine, then you should look to using reduce (Puppet4) to apply a similar function to a collection of values. In the case of having an array of hashes that you want to merge, you would do something like the following

$data = [ $h1, $h2, ... $hN ]
$merged_hash = $data.reduce({}) |$memo, $value| { $memo.merge($value) }
edit flag offensive delete link more



Thanks for your answer Daryl. I was hoping for an alternative to stdlib but it seems i'm out of luck(why not include it in puppet and be done with....i'm pretty sure i'm not the first person with this kind of use case) cheers

jondoe gravatar imagejondoe ( 2017-03-16 07:36:20 -0600 )edit

answered 2017-03-16 16:28:42 -0600

Josh gravatar image

If you can't use stdlib, you could create your own hash merge function.

A very basic example:

module Puppet::Parser::Functions
  newfunction(:hash_merge, :type => :rvalue, :arity => 2) do |args|
    hash1 = args[0]
    hash2 = args[1]
    return hash1

and call it from your manifest like:

$h1 = { 'a' => '1' }
$h2 = { 'b' => '2' }
$h3 = hash_merge($h1, $h2)
notice( $h3 )
edit flag offensive delete link more


Great point, I forgot to mention that, I got distracted in my answer!

DarylW gravatar imageDarylW ( 2017-03-17 06:48:45 -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


Asked: 2017-03-16 04:13:45 -0600

Seen: 2,394 times

Last updated: Mar 16 '17