How to pass a hash to a variable with Puppet Enterprise?

1,310 views
Skip to first unread message

Chris Neal

unread,
Apr 22, 2014, 12:40:08 PM4/22/14
to puppet...@googlegroups.com
Hi all,

This is most likely a noob question, so I apologize.  I've googled as well and was not able to find an answer to this seemingly basic question.
I'm using Puppet Enterprise 3.1.2 along with this module to install/manage Elasticsearch:  https://forge.puppetlabs.com/elasticsearch/elasticsearch/0.2.3

I've added the elasticsearch class to my node definition, and when I try and pass the hash to the 'config' variable, my runs fail with various errors about the parameter being a string, not a hash.  

I've tried:
 class { 'elasticsearch':
   config                   => {
     'node'                 => {
       'name'               => 'elasticsearch001'
     },
     'index'                => {
       'number_of_replicas' => '0',
       'number_of_shards'   => '5'
     },
     'network'              => {
       'host'               => $::ipaddress
     }
   }
 }
and also:

config                   => {
     'node'                 => {
       'name'               => 'elasticsearch001'
     },
     'index'                => {
       'number_of_replicas' => '0',
       'number_of_shards'   => '5'
     },
     'network'              => {
       'host'               => $::ipaddress
     }
   }
and also:
{
     'node'                 => {
       'name'               => 'elasticsearch001'
     },
     'index'                => {
       'number_of_replicas' => '0',
       'number_of_shards'   => '5'
     },
     'network'              => {
       'host'               => $::ipaddress
     }
All result in something like this:

====================
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: " config => { 'node' => { 'name' => 'elasticsearch001' }, 'index' => { 'number_of_replicas' => '0', 'number_of_shards' => '5' }, 'network' => { 'host' => $::ipaddress } }" is not a Hash. It looks to be a String at /etc/puppetlabs/puppet/modules/elasticsearch/manifests/init.pp:242 on node n6.example.com 

Warning: Not using cache on failed catalog 

Error: Could not retrieve catalog; skipping run
====================

How exactly would I pass a hash to this variable so that it will work?

Thanks so much.
Chris

jcbollinger

unread,
Apr 22, 2014, 2:40:25 PM4/22/14
to puppet...@googlegroups.com


On Tuesday, April 22, 2014 11:40:08 AM UTC-5, Chris Neal wrote:
Hi all,

This is most likely a noob question, so I apologize.  I've googled as well and was not able to find an answer to this seemingly basic question.
I'm using Puppet Enterprise 3.1.2 along with this module to install/manage Elasticsearch:  https://forge.puppetlabs.com/elasticsearch/elasticsearch/0.2.3

I've added the elasticsearch class to my node definition, and when I try and pass the hash to the 'config' variable, my runs fail with various errors about the parameter being a string, not a hash.  

I've tried:
 class { 'elasticsearch':
   config                   => {
     'node'                 => {
       'name'               => 'elasticsearch001'
     },
     'index'                => {
       'number_of_replicas' => '0',
       'number_of_shards'   => '5'
     },
     'network'              => {
       'host'               => $::ipaddress
     }
   }
 }
[...]

 
All result in something like this:

====================
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: " config => { 'node' => { 'name' => 'elasticsearch001' }, 'index' => { 'number_of_replicas' => '0', 'number_of_shards' => '5' }, 'network' => { 'host' => $::ipaddress } }" is not a Hash. It looks to be a String at /etc/puppetlabs/puppet/modules/elasticsearch/manifests/init.pp:242 on node n6.example.com 



Differences in whitespace are not significant in Puppet manifests, so as far as I can tell, all your attempts are equivalent.  I don't see anything wrong with them as such, but perhaps the context in which that declaration appears is causing the issue.  Or maybe the keys should be unquoted.  Following PL examples (http://docs.puppetlabs.com/puppet/3/reference/lang_datatypes.html#hashes), I don't quote my hash keys, but the docs do say they are strings, so I would expect quoting them to be valid.

Does that declaration appear, in that form, in a manifest file somewhere?  (If not, then please tell us what you are really doing.)  If putting the example declaration, verbatim, into a Puppet class and assigning that class to a node causes the compilation of the node's catalog to fail, then I'm sure PL would appreciate a bug report.  In the mean time, you could consider assigning the hash to a class variable of the containing class, and using that to configure Class['elasticsearch']:

$search_config = {

  node    => {
    name => 'elasticsearch001'
  },
  index   => {
    numberofreplicas => '0',
    numberofshards   => '5'

  },
  network => {
    host  => $::ipaddress
  }
}

class { 'elasticsearch':
  config => $search_config
}


Really, though, it would be much better form to to externalize the data -- i.e. to store the config hash in an Hiera data store under key 'elasticsearch::config'.  (If you do that then remember to format the hash appropriately for the relevant Hiera back-end.)



John

Chris Neal

unread,
Apr 23, 2014, 2:07:18 PM4/23/14
to puppet...@googlegroups.com
Hi John,

Thanks for the detailed reply.  That declaration appears on the Puppet Forge site for that module as an example for what has to use for that variable.  

I took your suggestion and just modified the default value for the variable and plugged that has in, and it works fine.

There seems to be something different in the syntax for a hash when entering it into the form data in Puppet Enterprise that I cannot figure out.  I tried removing extra spaces, and making the single quotes double ones, but that didn't work either.

If you or anyone else has some insight into getting a has accepted in PE, please share!

Thank you again for the help.
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/80f2036a-d437-4f4e-80b5-a39b3973d9d2%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Apr 24, 2014, 9:23:44 AM4/24/14
to puppet...@googlegroups.com


On Wednesday, April 23, 2014 1:07:18 PM UTC-5, Chris Neal wrote:
Hi John,

Thanks for the detailed reply.  That declaration appears on the Puppet Forge site for that module as an example for what has to use for that variable.  

I took your suggestion and just modified the default value for the variable and plugged that has in, and it works fine.

There seems to be something different in the syntax for a hash when entering it into the form data in Puppet Enterprise that I cannot figure out.  I tried removing extra spaces, and making the single quotes double ones, but that didn't work either.



See, THAT is exactly the kind of thing I was wondering about and looking for.  If you are providing the data by entering it into the console -- as opposed to putting it literally into a class declaration in a manifest file -- then everything makes sense.  In all likelihood, you are entering the parameter value into the console in a format that the console interprets as a string.  The docs (http://docs.puppetlabs.com/pe/latest/console_classes_groups.html#editing-class-parameters-on-nodes, "Supported Data Types" subsection) are not very expansive on this point, but it looks like maybe the console wants you to enclose hash values in backticks (`).  That's strangely inconsistent if true, but it would explain the observed behavior.


John

Reply all
Reply to author
Forward
0 new messages