Unable to access hash elements directly from templates

3,403 views
Skip to first unread message

Josh

unread,
Apr 11, 2012, 12:04:47 PM4/11/12
to puppet...@googlegroups.com
I am using puppet version 6.2 (the one that comes with squeeze) in case it makes a difference.

I am new to puppet and am having a problem getting access to the elements of a hash in my erb templates and no matter what I google for I cannot seem to find out what I am doing wrong. 

I have stripped everything back to example code to rule out other things, in reality I have split my files up a but to avoid the monolithic init.pp and nodes.pp. Anyway...

my nodes.pp has this in it:


  # Test code
  $myhash = { 
    key => { 
      subkey1 => 'foo',
      subkey2 => 'bar'
    }
  }

  include mymodule
}

Then in the modules init.pp I have:

class mymodule {
  # Test code
  notice($myhash[key][subkey1])
  notice($myhash[key][subkey2])
  file { "/tmp/test":
    ensure => present,
    content => template("mymodule/test.erb"),
  }
}

If I run puppet against this the two notice lines work fine so I have access to the data within the module, the problem is getting it into the template. I tried a few different syntaxes to no effect:

So first I tried a template like so:
TEST: <%= $myhash[key][subkey1] %>

This gave me the error:
err: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template mymodule/test.erb: Could not find value for 'key' at /etc/puppet/modules/mymodule/manifests/init.pp:7 on node pc01.domain.com

If I quote the string in the template (i.e. "<%= $myhash[key][subkey1] %>") then it just spits out that string into the file rather than interpreting the data.

I have similar errors if I try to use scope.lookupvar to explicitly specify where things are too.

So, what am I doing wrong or is it just not possible to pick a single hash element and put it into a template.

Thanks
Josh
p.s. my Ruby sucks so if I have to go down that route it'll need to be a noddy explanation

Gary Larizza

unread,
Apr 11, 2012, 12:14:10 PM4/11/12
to puppet...@googlegroups.com
Hey Josh,

Don't use the $ for variables in Templates - Puppet variables and arrays are accessed as local variables/arrays in templates.  So try this:  TEST: <%= myhash[key][subkey1] %>


--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/nSPGiquUVBIJ.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.



--

Gary Larizza
Professional Services Engineer
Puppet Labs

Josh

unread,
Apr 12, 2012, 4:02:59 AM4/12/12
to puppet...@googlegroups.com
On Wednesday, 11 April 2012 17:14:10 UTC+1, Gary Larizza wrote:
Don't use the $ for variables in Templates - Puppet variables and arrays are accessed as local variables/arrays in templates.  So try this:  TEST: <%= myhash[key][subkey1] %>


Ah, I did try that before. I get the error:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template mymodule/test.erb: Could not find value for 'key' at /etc/puppet/modules/mymodule/manifests/config.pp:22 on node pc01.domain.com

Thanks
Josh

Josh

unread,
Apr 12, 2012, 6:18:43 AM4/12/12
to puppet...@googlegroups.com
Sorry, that should be:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template mymodule/test.erb: Could not find value for 'key' at /etc/puppet/modules/mymodule/manifests/init.pp:7 on node pc01.domain.com

(i.e. it errors on the content => ... line)

Josh

Josh

unread,
Apr 12, 2012, 6:34:56 AM4/12/12
to puppet...@googlegroups.com
Eventually I worked this out (nothing like trying every possible combination eh), solution was to remove the $ at the start as you said and also quote the keys in single quotes like so:

TEST: <%= myhash['key']['subkey1'] %>

Thanks again for your help
Josh
Reply all
Reply to author
Forward
0 new messages