Copying a hash with changes

448 views
Skip to first unread message

Tom Limoncelli

unread,
Feb 28, 2015, 1:28:13 PM2/28/15
to puppet...@googlegroups.com
I want to copy a hash to a variable but change some settings along the way.

For example:

I have $haproxy::params::global_options (which is a hash) and I want
to create a copy with some changes.

I tried this:
$global_options = $haproxy::params::global_options += {
'log' => "${log_ip} local0",
}
But that gives me:
Error: Syntax error at '+='; expected '}' at ...

If I do:
$global_options = $haproxy::params::global_options
$global_options['log'] = "${log_ip} local0"
This gives me:
Error: Assigning to the hash 'global_options' with an existing key
'log' is forbidden

Suggestions?

Tom

--
Email: t...@whatexit.org Work: tlimo...@StackOverflow.com
Skype: YesThatTom
Blog: http://EverythingSysadmin.com

Dan White

unread,
Feb 28, 2015, 2:25:29 PM2/28/15
to Puppet Users Mailing List
Make a hash with the update in it and then merge them (needs stdlib)

Like this:

$new_hash = {
  'log' => "${log_ip} local0",
}

$global_options = merge ( $haproxy::params::global_options, $new_hash )


• 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


-- 
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAHVFxgnmdYrNOUdujza-EE0M5KyMZDR%3Dzt3wB-5tvbh8%3DaC4-w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.” 
Bill Waterson (Calvin & Hobbes)

Garrett Honeycutt

unread,
Feb 28, 2015, 2:42:34 PM2/28/15
to puppet...@googlegroups.com
On 2/28/15 1:27 PM, Tom Limoncelli wrote:
> I want to copy a hash to a variable but change some settings along the way.
>
> For example:
>
> I have $haproxy::params::global_options (which is a hash) and I want
> to create a copy with some changes.
>
> I tried this:
> $global_options = $haproxy::params::global_options += {
> 'log' => "${log_ip} local0",
> }
> But that gives me:
> Error: Syntax error at '+='; expected '}' at ...
>
> If I do:
> $global_options = $haproxy::params::global_options
> $global_options['log'] = "${log_ip} local0"
> This gives me:
> Error: Assigning to the hash 'global_options' with an existing key
> 'log' is forbidden
>
> Suggestions?
>
> Tom
>

Hi Tom,

Suggest using a template as it provides a way to hack around issues
involving munging data and types by going straight to ruby. You could
use the inline_template() function, though I prefer having it in a
separate file for readability and for syntax checking.

Here's a quick and dirty hack that might work for you. Notice in the
template that the ruby code is between <% %> and the interpolation that
is being returned to $hn in your manifest is between <%= %>.

manifests/init.pp

class tl (
) {

$h = {
'k' => 'v',
}

$hn = template('tl/hack.erb')

notify { 'asdf':
message => "h = ${h}",
}
notify { "hn = ${hn}":
require => Notify['asdf'],
}
}


# templates/hack.erb
<% ht = {}
ht = @h.merge 'log' => "#{@ipaddress} local0"%>
<%= ht %>

# puppet apply -v tests/init.pp
Notice: h = {"k"=>"v"}
Notice: hn =
{"k"=>"v", "log"=>"10.0.1.3 local0"}

Best regards,
-g

--
Garrett Honeycutt
@learnpuppet
Puppet Training with LearnPuppet.com
Mobile: +1.206.414.8658

Henrik Lindberg

unread,
Mar 1, 2015, 6:37:52 PM3/1/15
to puppet...@googlegroups.com
On 2015-28-02 19:27, Tom Limoncelli wrote:
> I want to copy a hash to a variable but change some settings along the way.
>
> For example:
>
> I have $haproxy::params::global_options (which is a hash) and I want
> to create a copy with some changes.
>
> I tried this:
> $global_options = $haproxy::params::global_options += {
> 'log' => "${log_ip} local0",
> }

In Puppet 4.0 (and in 3x with parser=future) you can do this (i.e. merge
a hash) directly in the Puppet Language:

$global_options = $haproxy::params::global_options + {
'log' => "{log_ip} local0"
}


- henrik


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Tom Limoncelli

unread,
Mar 4, 2015, 10:46:59 PM3/4/15
to puppet...@googlegroups.com
On Sat, Feb 28, 2015 at 2:25 PM, Dan White <d_e_...@icloud.com> wrote:
$new_hash = {
  'log' => "${log_ip} local0",
}

$global_options = merge ( $haproxy::params::global_options, $new_hash )

Thanks, Dan!  I'll do that while I wait for Puppet 4.0 (or when I can use the future parser).
 
On Sun, Mar 1, 2015 at 6:37 PM, Henrik Lindberg <henrik....@cloudsmith.com> wrote:
In Puppet 4.0 (and in 3x with parser=future) you can do this (i.e. merge a hash) directly in the Puppet Language:

$global_options = $haproxy::params::global_options + {
    'log' => "{log_ip} local0"
  }

Henrik, that's great!  I look forward to that addition to Puppet!

Tom
Reply all
Reply to author
Forward
0 new messages