Using hiera lookups with defined types

2,140 views
Skip to first unread message

Ken Weiss

unread,
Aug 23, 2013, 7:01:19 PM8/23/13
to puppet...@googlegroups.com
Hi all,

I am trying to use hiera and .yaml files to load parameters into my declared classes. For basic classes it works great. For example:

class cron_jobs::logrotate ( $hour, $minute, $weekday ) {...

node fqdn: test.my.com

I declare the class for my node very simply:

class {'cron_jobs::logrotate': }

If I create a file in the default hiera data directory called test.my.com.yaml and include this:

cron_jobs::logrotate::hour: 5

then sure enough, when I do a puppet apply the cron job is created to run at 5:00 AM.


My problem is extending this to work with defined types. Let's say my defined type looks like this when I declare it:

cron_jobs::logrotate::user_cron {'kenweiss' :}

What do I put in my .yaml file in order to get Puppet to load the parameters automatically?

I tried a couple of things...

cron_jobs::logrotate::user_cron::kenweiss::hour 5
cron_jobs::logrotate::user_cron[kenweiss]::hour 5

Neither worked, and I can't think of any other reasonable syntax. Can anyone point me in the right direction?

--Ken Weiss

Dan White

unread,
Aug 23, 2013, 8:22:01 PM8/23/13
to puppet...@googlegroups.com
Does cron_jobs::logrotate::user_cron have just one parameter ?
Or are there other parameters that are given default values in the type definition ?

--
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 post to this group, send email to puppet...@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.

Chris McDermott

unread,
Aug 24, 2013, 1:17:05 AM8/24/13
to puppet...@googlegroups.com
Well normally you would use hiera_hash() and create_resources() to do it, like this:

# yaml data source
---
cron_jobs::logrotate::users
  kenweiss:
    hour => 5
  tomjones:
    hour => 3


# pp file
---
class cron_jobs::logrotate ( $hour, $minute, $weekday ) {
  ...
  $user_cron_options = hiera_hash('cron_jobs::logrotate::users', false)
  if $user_cron_options{
    create_resources('cron_jobs::logrotate::user_cron', $user_cron_options)
  }
}

One thing I don't know is how to do that for defined types that do not have any parameters.  Unfortunately create_resources requires a hash for the second argument, so you have to use hiera_hash to pull in the data, so it has to be a hash in hiera. And I haven't figured out how to define an empty hash yet. Or transform an array into a hash that would work for create_resources.

/shrug

Chris


--

Ellison Marks

unread,
Aug 26, 2013, 2:56:08 PM8/26/13
to puppet...@googlegroups.com
Agreed, create resources is the way to create defined types using hiera data. As a note though, if you need to create a bunch of defined types without parameters, if, for example, you're only relying on the name parameter, you can pass an array of titles to a declaration. http://docs.puppetlabs.com/puppet/3/reference/lang_resources.html#array-of-titles

yaml:
---
logrotate_users:
  - alice
  - bob
  - carol

pp:
$logrotate_users = hiera_array('logrotate_users')
cron_jobs::logrotate::users { $logrotate_users: }

Ken Weiss

unread,
Aug 26, 2013, 3:00:40 PM8/26/13
to puppet...@googlegroups.com
Thanks to Chris's suggestion I have this mostly working. I need to add a few final touches and then I'll post a closing note with full examples of the solution. I really appreciate the help. I'm just getting started with Puppet, barely understood defined resources, and didn't even know that create_resources existed.

--Ken

------------------------------------------------------------
Ken Weiss                                 ken....@ucop.edu
UC Office of the President              510-587-6311 (office)
California Digital Library              916-905-6933 (mobile)
UC Curation Center
415 20th Street, 4th Floor
Oakland, CA 94612


You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/Lual4Uxo3Ds/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.

Stuart Cracraft

unread,
Aug 26, 2013, 3:16:15 PM8/26/13
to puppet...@googlegroups.com
Proceed forward through



--Stuart
RHCE/RHCSA/Oracle DBA/Sun/Mensa

Ken Weiss

unread,
Aug 27, 2013, 12:50:19 PM8/27/13
to puppet...@googlegroups.com
As promised, for those of you playing along at home, here's what I came up with.

The complicating factor in my situation is that I need to write Puppet code that will execute correctly today when run using 'puppet apply...' as a non-priviliged user, but still work correctly at some future date when my systems group sets up a proper Puppet infrastructure with a master and agents on the managed nodes running as the root user. The solution I came up with is inelegant, but it works.

I have one module that works whether it is called as root or as an unprivileged user. It is a defined resource.

File compression module:

define compress_roleid::roleid ( $hour = 0,
                          $minute = 22,
                          $weekday = 0,
                          $compress_files = "",
                          $user = $title, ) {

[some Puppet code goes here to create a bash script, some configuration files and a cron job]
}


I have a file saved under .../hiera/node/[fqdn].yaml with the following data:

---
# data to be used when Puppet is run as the dpr2 user
dpr2::compress:
  dpr2:
    compress_files:
      - "/dpr2/apps/metacat33181/tomcat/logs"
      - "/dpr2/postgres/log"
    hour: 0
    minute: 20
    weekday: 0

# data to be used when Puppet is run as the dpr2store user
dpr2store::compress:
  dpr2store:
    compress_files:
      - "/dpr2store/apps/fixity33143/tomcat/logs"
      - "/dpr2store/apps/storage35121/tomcat/logs"
    hour: 0
    minute: 25
    weekday: 0

# data to be used when Puppet is run as root
compress_roleid::roleid::user:
  dpr2:
    user: "dpr2"
    compress_files:
      - "/dpr2/apps/metacat33181/tomcat/logs"
      - "/dpr2/postgres/log"
    hour: 0
    minute: 25
    weekday: 0
  dpr2store:
    user: "dpr2store"
    compress_files:
      - "/dpr2store/apps/fixity33143/tomcat/logs"
      - "/dpr2store/apps/storage35121/tomcat/logs"
    hour: 0
    minute: 25
    weekday: 0

My nodes.pp file contains the following:

node 'uc3-mrt-store-stg.cdlib.org' inherits basenode {
  if $::id == "root" {
    # Retrieve all parameters from .../puppet/hiera/node/[fqdn].yaml
    $logrotate_roleid_options = hiera_hash('logrotate_roleid::roleid::user', false)
    $compress_roleid_options = hiera_hash('compress_roleid::roleid::user', false)
    # If parameters are found, create the resources
    if $logrotate_roleid_options {
      create_resources('logrotate_roleid::roleid', $logrotate_roleid_options)
    }
    if $compress_roleid_options {
      create_resources('compress_roleid::roleid', $compress_roleid_options)
    }
  }
  else {
    # Retrieve per-user parameters from .../puppet/hiera/node/[fqdn].yaml
    $per_user_compress = hiera_hash("${::id}::compress", false)
    $per_user_logrotate = hiera_hash("${::id}::logrotate", false)
    # If parameters are found, create the resources
    if $per_user_compress {
      create_resources('compress_roleid::roleid', $per_user_compress)
    }
    if $per_user_logrotate {
      create_resources('logrotate_roleid::roleid', $per_user_logrotate)
    }
  }
}

When Puppet is run as an unprivileged user, the parameters for the create_resources statement use the ${::id} as the key to the YAML hash. Puppet must be run twice, once as the dpr2 user, and again as the dpr2store user.

If, on the other hand, Puppet is run as root, then the hash data keyed as 'compress_roleid::roleid' is used, which contains information to set up file compression for both users in a single operation.

I haven't been able to fully test this as root, but when I edit the nodes.pp file to force execution of the create_resources statement using the root YAML hash, the output of puppet apply --noop certainly looks like it will do the right thing. And when I run the script as dpr2 or dpr2store it definitely does do the right thing.

Thanks to everyone, and especially Chris, for helping me over the rough patch.

--Ken

------------------------------------------------------------
Ken Weiss                                 ken....@ucop.edu
UC Office of the President              510-587-6311 (office)
California Digital Library              916-905-6933 (mobile)
UC Curation Center
415 20th Street, 4th Floor
Oakland, CA 94612

From: Chris McDermott <csmcd...@gmail.com>
Reply-To: "puppet...@googlegroups.com" <puppet...@googlegroups.com>
Date: Friday, August 23, 2013 10:17 PM
To: "puppet...@googlegroups.com" <puppet...@googlegroups.com>
Subject: Re: [Puppet Users] Using hiera lookups with defined types

Reply all
Reply to author
Forward
0 new messages