Ask Your Question

modifying hash key name within the puppet manifest

asked 2016-10-07 12:42:03 -0600

mlanghor gravatar image

updated 2016-10-07 12:50:25 -0600

I'm working with a bit older version of puppet still (3.x), and am having an issue with hash key names conflicting.

I have a defined type, that's being called from multiple other modules. In the external module I'm using hierahash to retrieve the data used in the defined type using createresoureces(type,$vars). The problem is these hash key names can be the same.

How can I modify the hash name within the manifest? While we could say "use a unique name" it's likely to conflict again at some point.

for a hash similar to the following: "WSDL" => { uri => 'sst/wsdl', timeout=> '300',}

add a string to WSDL, making it WSDL_myapp

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-10-07 20:26:24 -0600

DarylW gravatar image

updated 2016-10-07 20:52:14 -0600

I'm not sure the best route to follow in puppet, you may want to pass the hash into a custom function, and handle the translation in ruby.

If you are doing it in ruby, you could use map to adjust the keys like this. (example from IRB)

2.3.0 :002 > Hash[{'one':'1', 'two':'2'}.map{|k,v| ["number-#{k}", v]}                                                                               
 => {"number-one" => "1", "number-two" => "2" }

Or, an example that mirrors your input.

2.3.0 :005 > h = {"WSDL" => { 'uri' => 'sst/wsdl', 'timeout'=> '300',}}                                                                      
 => {"WSDL"=>{"uri"=>"sst/wsdl", "timeout"=>"300"}} 
2.3.0 :006 > appname = 'myapp'
 => "myapp" 
2.3.0 :011 > Hash[{|k,v| ["#{k}-#{appname}", v] }]                                                                                      
 => {"WSDL-myapp"=>{"uri"=>"sst/wsdl", "timeout"=>"300"}}

Converting that into the puppet format custom functions documentation

module Puppet::Parser::Functions
  newfunction(:append_hash_key) do |args|
    hash = args[0]
    append_str = args[1]
    Hash[{|k,v| ["#{k}-#{append_str}", v] }]

You would use that to pre-convert the hash that you loaded in from hiera, and then pass that to create_resources afterwards.

$vals = hiera('my_hash')
$appname = 'appname'
$translated_keys = append_hash_key($vals, $appname)
create_resources(mytype, $translated_keys
edit flag offensive delete link more

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: 2016-10-07 12:42:03 -0600

Seen: 469 times

Last updated: Oct 07 '16