Accessing hash values in defined type

19 views
Skip to first unread message

Bret Wortman

unread,
Nov 13, 2013, 7:57:35 AM11/13/13
to puppet...@googlegroups.com
I'm sure this is so simple I'm just not seeing it. I have an array of hashes of filenames & modes defined in hiera (the actual problem is a tad more complex, but for simplicity, if I can solve this, I can solve the bigger problem):

files:
  - name: /etc/skel/.bashrc
    mode: 600
  - name: /etc/sysctl.conf
    mode: 600

and so on.

I then have a class which loads this hash and wants to execute a defined type for it, basically enforcing the mode for each file:

define compliance::file {
  file { "$title":
    mode => ???
  }
}

And that's where I'm stuck -- how on earth do I get at the "mode" value of the array of hashes? Is there an easier way to do this that allows me to add additional hash keys for various files later?

Thanks!

Gavin Williams

unread,
Nov 13, 2013, 9:47:15 AM11/13/13
to puppet...@googlegroups.com
Bret

Sounds like 'create_resources' might be a good fit here... Take a look at http://four-eyes.net/2013/09/puppet-passing-a-hash-of-variables-to-a-defined-type/

Alternatively, if you can't use 'create_resources' you probably want to add a parameter to the defined type, and then use the indexing syntax as defined here: http://docs.puppetlabs.com/puppet/3/reference/lang_datatypes.html#hashes to call out specific keys from said parameter.

HTH
Gav

Bret Wortman

unread,
Nov 13, 2013, 10:04:38 AM11/13/13
to puppet...@googlegroups.com
Thanks. I'll give that a look.

I got it working like this:

define compliance::file {
  $fmode=$title['mode']
  $fname=$title['name']

  file { $fname:
    path => $fname,
    mode => $fmode,
  }
}

And then I can reference it like this:

compliance::file { $files: }

But I'd like to find something cleaner. Thanks for the tip, Gavin.
Reply all
Reply to author
Forward
0 new messages