About | FAQ | Help
Ask Your Question

Iterate through hash to change a specific key value

asked 2018-04-19 07:27:48 -0500

Jhonny gravatar image

updated 2018-04-19 07:56:22 -0500

DarylW gravatar image

I have this variable in Hiera:

    dsc_username: 'myuser1'
    dsc_description: 'my user account'
    dsc_ensure: 'present'
    dcs_password: 'some encrypted value'
    dsc_username: 'myuser1'
    dsc_ensure: 'present'
    dcs_password: 'some encrypted value'
    dsc_disabled: 'true'

I would like to create resources of the following format in the Puppet manifests:

dsc_user { 'myuser1':
  dsc_username             => 'myuser1',
  dsc_description          => ''my user account',
  dsc_ensure               => present,
  dsc_password             => {
    'user' => 'myuser1',
    'password' => Sensitive('some encrypted value')
  dsc_disabled             => true,

If we did not have the special password format, this would be straight forward, something like this:

each($myUsernames) |$name, $data|{
    * => $data

Is there and easy way to iterate over the hiera hash and dynamically create de resource?

edit retag flag offensive close merge delete


If you consistently have the same encrypted password format, you can 'wrap' the dsc_user class with a dsc_user_enc class that would take the encrypted string as it's input, and remap that into the inputs above. You can probably just skip the class and directly set up all of the mappings instead of

DarylW gravatar imageDarylW ( 2018-04-19 08:00:14 -0500 )edit

.. using the shorthand 'glob' operator to do your mappings. The other potential thing that you can do is 'map' over the collection, and do the transformation of the encrypted password area, and then take the result of that map and use your above 'spat' operator to fill them in.

DarylW gravatar imageDarylW ( 2018-04-19 08:01:43 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-04-19 09:53:40 -0500

DarylW gravatar image

updated 2018-04-19 09:55:03 -0500

Note: I haven't tried this in actual code.

Relating to my above comment, I would probably just manually remap all of the fields, like the example below. This has the advantage of being obvious and straightforward, and passes the '3am' test where you want things to be easy to figure out.

each($myUsernames) | $name, $data | {
  dsc_user { $name:
    dsc_username             => $data['dsc_username'],
    dsc_description          => $data['dsc_description'],
    dsc_ensure               => $data['dsc_ensure'],
    dsc_password             => {
      'user' => $data['dsc_username'],
      'password' => Sensitive($data['dsc_password'])
    dsc_disabled             => $data['dsc_disabled'],

If you wish to have varied parameters in your hiera array and only want to remap the value of your password into a decrypted version, you could do something like this.

$myUsernamesRemapedPasswords = map($myUsernames) |$user| { 
     # from https://puppet.com/docs/puppet/4.10/lang_data_hash.html
     # if you add a hash to a hash it replaces the key from the right 
     # most hash, that then returns the result for that user into the
     # same 'spot' in the returned array.  

     # The right hand side is generating a hash with just the 'modified' password.
     $user + { 'dsc_password': Sensitive($user['dsc_password']) } 

and then follow it up with your above splat operator (if that works).

each($myUsernamesRemapedPasswords) |$name, $data|{
    * => $data

The splat operator may give you problems with your nested password operator, which would require you do do something else anyway. I'm not sure how the splat operator would handle nested hashes , so you may be 'stuck' with the explicit mapping at the top of this answer.

Hope that helps!

edit flag offensive delete link more


Thanks! I will give it a try and report on the results.

Jhonny gravatar imageJhonny ( 2018-04-20 08:57:16 -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

1 follower


Asked: 2018-04-19 07:27:48 -0500

Seen: 31 times

Last updated: Apr 19