Ask Your Question

help with hashes inside hiera

asked 2015-02-18 19:08:27 -0500

sakodak gravatar image

I feel like I'm missing something fundamental, but here goes.

I'm trying to build a generic "packages" module that I can wrap around package, files and services resources. I want to do this so that I can virtualize the packages and realize them where I want to. Some things I'm trying to take into consideration are:

1) some packages may have an associated service
2) services can have multiple config files
3) users need to exist before the packages are installed (so that UIDs and GIDs can stay in sync across nodes.)

I don't care so much about 3 right now. I have accounts defined in another defined type which is working. I'll need to link this to that later, but I want to focus on the config files for now.

I'm having trouble in config.pp. I want to take service_files and create file resources with the data available in hiera (I also want to be able to subscribe / notify files / services if necessary.) I don't know how to extract that and "iterate" over it (I know iterate is a bad word in puppet-speak, but I don't know how else to put it.)

I guess more succinctly:

How do I get to the owner, group, mode etc... entries for each service_files? My module structure looks like this:


My hiera data looks like this:

    service_name: snmpd
      - allsystems
      - monitoring
    # user here for testing only
    required_user: testuser
    service_state: running
    service_enabled: enabled
        owner: root
        group: root
        mode: 0644
        template_name: snmpd.conf.erb
        subscribe: true
        owner: root
        group: root
        mode: 0600
        template_name: snmptrapd.conf.erb
        subscribe: true
        owner: root
        group: root
        mode: 0644
        template_name: snmpd.erb
        subscribe: true

Forgive the debugging stuff in this code, please. :/

Here's what I have in config.pp:

define packages::config (
  $service_name    = $package::params::service_name,
  $service_enabled = $package::params::service_enabled,
  $service_state   = $package::params::service_state,
  $service_files   = $package::params::service_files,
  $required_user   = $package::params::required_user,
  $tag             = $package::params::tag,
) {
  if $service_name != undef {
    notify { "service files has value ${service_files}": }
    # obviously I'm doing this wrong and this is exceptionally
    # stupid, but I've tried a lot of stuff and this is just the current state
    file { $service_files:
      ensure => present,
      owner  => $service_files::owner,
      group  => $service_files::group,
      mode   => $service_files::mode,
      tag    => $service_files::tag,

Here's init.pp:

class packages {
#  anchor { 'packages::begin': } ->
#         { '::packages::params': } ->
#         { '::packages::install': } ->
#         { '::packages::config': } ->
#         { '::packages::service': } ->
#  anchor { 'packages::end':}
  notify { 'we are in init.pp': }
  notify { 'calling hiera': }
  notify { 'calling your mom': }
  $mypackages = hiera_hash('packages::package',{})
  notify { 'creating resources': }
  notify { "my packages are ${mypackages}": }
  create_resources('@packages::install', $mypackages)
  create_resources('@packages::config', $mypackages)
  create_resources('@packages::service', $mypackages)
  Packages::Install <| title == net-snmp |>
  Packages::Config <| title == net-snmp |>
  Packages::Service <| title == net-snmp |>

And here's package.pp:

define packages::package (
  $required_user   = $packages::params::service_user,
  $service_name    = $packages::params::service_name,
  $service_state   = $packages::params::service_state,
  $service_enabled = $packages::params::service_enabled ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-02-24 14:33:28 -0500

lorcutt gravatar image

It looks like class packages does the hiera call and gets $mypackages populated with the hash. If the goal is to get at the parameters for each service file, then you could do something like

$mypackages['service_files'].keys.each | $service_file |
   $owner = $mypackages['service_files'][$service_file]['owner']

Hope that helps

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: 2015-02-18 19:08:27 -0500

Seen: 173 times

Last updated: Feb 24 '15