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 -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Does cron_jobs::logrotate::user_cron have just one parameter ? Or are there other parameters that are given default values in the type definition ? On Aug 23, 2013, at 7:01 PM, Ken Weiss wrote:> 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 > > -- > 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+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users. > For more options, visit https://groups.google.com/groups/opt_out.-- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@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
2013-Aug-24 05:17 UTC
Re: [Puppet Users] Using hiera lookups with defined types
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 On Fri, Aug 23, 2013 at 5:01 PM, Ken Weiss <ken.weiss@ucop.edu> wrote:> 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 > > -- > 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+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users. > For more options, visit https://groups.google.com/groups/opt_out. >-- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Ellison Marks
2013-Aug-26 18:56 UTC
Re: [Puppet Users] Using hiera lookups with defined types
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: } On Friday, August 23, 2013 10:17:05 PM UTC-7, mcd wrote:> > 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 > > > On Fri, Aug 23, 2013 at 5:01 PM, Ken Weiss <ken....@ucop.edu <javascript:> > > wrote: > >> 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 >> >> -- >> 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 <javascript:>. >> To post to this group, send email to puppet...@googlegroups.com<javascript:> >> . >> Visit this group at http://groups.google.com/group/puppet-users. >> For more options, visit https://groups.google.com/groups/opt_out. >> > >-- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
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.weiss@ucop.edu<mailto:ken.weiss@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: Ellison Marks <gtyaoi@gmail.com<mailto:gtyaoi@gmail.com>> Reply-To: "puppet-users@googlegroups.com<mailto:puppet-users@googlegroups.com>" <puppet-users@googlegroups.com<mailto:puppet-users@googlegroups.com>> Date: Monday, August 26, 2013 11:56 AM To: "puppet-users@googlegroups.com<mailto:puppet-users@googlegroups.com>" <puppet-users@googlegroups.com<mailto:puppet-users@googlegroups.com>> Subject: Re: [Puppet Users] Using hiera lookups with defined types 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: } On Friday, August 23, 2013 10:17:05 PM UTC-7, mcd wrote: 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 On Fri, Aug 23, 2013 at 5:01 PM, Ken Weiss <ken....@ucop.edu<javascript:>> wrote: 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<http://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 -- 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<javascript:>. To post to this group, send email to puppet...@googlegroups.com<javascript:>. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out. -- 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+unsubscribe@googlegroups.com<mailto:puppet-users+unsubscribe@googlegroups.com>. To post to this group, send email to puppet-users@googlegroups.com<mailto:puppet-users@googlegroups.com>. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out. -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Stuart Cracraft
2013-Aug-26 19:16 UTC
Re: [Puppet Users] Using hiera lookups with defined types
Proceed forward through http://docs.puppetlabs.com/learning/index.html --Stuart RHCE/RHCSA/Oracle DBA/Sun/Mensa On Aug 26, 2013, at 12:00 PM, Ken Weiss <ken.weiss@ucop.edu> wrote:> 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.weiss@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: Ellison Marks <gtyaoi@gmail.com> > Reply-To: "puppet-users@googlegroups.com" <puppet-users@googlegroups.com> > Date: Monday, August 26, 2013 11:56 AM > To: "puppet-users@googlegroups.com" <puppet-users@googlegroups.com> > Subject: Re: [Puppet Users] Using hiera lookups with defined types > > 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: } > > On Friday, August 23, 2013 10:17:05 PM UTC-7, mcd wrote: >> >> 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 >> >> >> On Fri, Aug 23, 2013 at 5:01 PM, Ken Weiss <ken....@ucop.edu> wrote: >>> 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 >>> -- >>> 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. > > -- > 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+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users. > For more options, visit https://groups.google.com/groups/opt_out. > -- > 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+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users. > For more options, visit https://groups.google.com/groups/opt_out.-- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
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.weiss@ucop.edu<mailto:ken.weiss@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 <csmcdermott@gmail.com<mailto:csmcdermott@gmail.com>> Reply-To: "puppet-users@googlegroups.com<mailto:puppet-users@googlegroups.com>" <puppet-users@googlegroups.com<mailto:puppet-users@googlegroups.com>> Date: Friday, August 23, 2013 10:17 PM To: "puppet-users@googlegroups.com<mailto:puppet-users@googlegroups.com>" <puppet-users@googlegroups.com<mailto:puppet-users@googlegroups.com>> Subject: Re: [Puppet Users] Using hiera lookups with defined types 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 -- 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+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.