Chris McDermott
2013-Aug-23 15:47 UTC
[Puppet Users] Test the result of a hiera_hash() lookup
Hi folks, Is there a way to test the result of a hiera_hash() lookup in a manifest? I am trying to use hiera to do most of my configuration, like this for example: mysql::grants: ''user1@localhost'': privileges: - select_priv ''user2@localhost'': privileges: - select_priv - insert_priv - lock_tables_priv I have a wrapper class that then does this: class custom::mysql::grants { $grantoptions = hiera_hash(''mysql::grants'') create_resources(''database_grant'', $grantoptions) } That works fine as long as I include the custom::mysql::grants class. But right now I can only include that class on nodes where I define explicit grants. If I don''t have the mysql::grants hash defined for that node in hiera, puppet complains: Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find data item mysql::grants in any Hiera data file and no default supplied at /path/to/custom/manifests/mysql.pp:34 I tried testing the $grantoptions variable after calling hiera_hash() but that''s too late - it''s the hiera_hash function itself that produces that error. I would love to be able to just include this wrapper class globally, and then have it only run create_resources if it actually finds a usable hash for that node, so I don''t have as many includes on each individual node. Any thoughts? Thanks! Chris -- 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.
Dan White
2013-Aug-24 00:16 UTC
Re: [Puppet Users] Test the result of a hiera_hash() lookup
Try this (works for me): class custom::mysql::grants { $grantoptions = hiera_hash(''mysql::grants’, false) if $grantoptions { create_resources(''database_grant'', $grantoptions) } } http://docs.puppetlabs.com/references/latest/function.html#hierahash … a default argument in the second position, providing a hash to be returned in the absence of any matches for the key argument By using “false” as a default, you will eliminate the error you are getting and the resources will be creates only if they are defined in Hiera. Share & Enjoy ! On Aug 23, 2013, at 11:47 AM, Chris McDermott wrote:> Hi folks, > > Is there a way to test the result of a hiera_hash() lookup in a manifest? I am trying to use hiera to do most of my configuration, like this for example: > > mysql::grants: > ''user1@localhost'': > privileges: > - select_priv > ''user2@localhost'': > privileges: > - select_priv > - insert_priv > - lock_tables_priv > > I have a wrapper class that then does this: > > class custom::mysql::grants { > $grantoptions = hiera_hash(''mysql::grants'') > create_resources(''database_grant'', $grantoptions) > } > > That works fine as long as I include the custom::mysql::grants class. But right now I can only include that class on nodes where I define explicit grants. If I don''t have the mysql::grants hash defined for that node in hiera, puppet complains: > > Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find data item mysql::grants in any Hiera data file and no default supplied at /path/to/custom/manifests/mysql.pp:34 > > I tried testing the $grantoptions variable after calling hiera_hash() but that''s too late - it''s the hiera_hash function itself that produces that error. I would love to be able to just include this wrapper class globally, and then have it only run create_resources if it actually finds a usable hash for that node, so I don''t have as many includes on each individual node. > > Any thoughts? > > Thanks! > > Chris > > -- > 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.