Josh
2012-Apr-11 16:04 UTC
[Puppet Users] Unable to access hash elements directly from templates
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:
node ''pc01.domain.com'' {
# 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
--
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-users@googlegroups.com.
To unsubscribe from this group, send email to
puppet-users+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en.
Gary Larizza
2012-Apr-11 16:14 UTC
Re: [Puppet Users] Unable to access hash elements directly from templates
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] %> On Wed, Apr 11, 2012 at 9:04 AM, Josh <josh@chickenmonkey.co.uk> wrote:> 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: > > node ''pc01.domain.com'' { > > # 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 > > -- > 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-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@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 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Josh
2012-Apr-12 08:02 UTC
Re: [Puppet Users] Unable to access hash elements directly from templates
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 -- 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/-/pOIGkNnuEo4J. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Josh
2012-Apr-12 10:18 UTC
Re: [Puppet Users] Unable to access hash elements directly from templates
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 -- 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/-/VFGB1obBa8QJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Josh
2012-Apr-12 10:34 UTC
Re: [Puppet Users] Unable to access hash elements directly from templates
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 -- 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/-/QznbDyO5SDAJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.