inline template variable access

255 views
Skip to first unread message

Brandon Metcalf

unread,
Sep 5, 2014, 1:22:37 PM9/5/14
to puppet...@googlegroups.com
Using puppet 3.4.3 and getting the following warning:

Variable access via 'java_home_var' is deprecated. Use '@java_home_var' instead. template[inline]:1

from the following line:

$java_home     = inline_template('<%= scope[@java_home_var] %>')

Any ideas?  Thanks.

--
Brandon Metcalf | Technical Operations


Cloud-based performance monitoring

  

Felix Frank

unread,
Sep 6, 2014, 12:46:04 PM9/6/14
to puppet...@googlegroups.com
On 09/05/2014 07:22 PM, Brandon Metcalf wrote:
Using puppet 3.4.3 and getting the following warning:

Variable access via 'java_home_var' is deprecated. Use '@java_home_var' instead. template[inline]:1

from the following line:

$java_home     = inline_template('<%= scope[@java_home_var] %>')

Any ideas?  Thanks.

It's a little confusing, yes. But I suspect the problem is the scope[] expression. Have you tried scope.lookupvar(@java_home_var) instead?

Spencer Krum

unread,
Sep 6, 2014, 5:09:42 PM9/6/14
to puppet...@googlegroups.com
The correct way is simply

$java_home = $java_home_var

Barring that, this is the correct way to use a template:

$java_home = inline_template('<%= @java_home_var %>')

While not deprecated, scope.lookupvar is the wrong way to do variable access in templates.


--
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/540B3A44.50407%40Alumni.TU-Berlin.de.

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



--
Spencer Krum
(619)-980-7820

Felix Frank

unread,
Sep 7, 2014, 10:39:56 AM9/7/14
to puppet...@googlegroups.com
On 09/06/2014 11:09 PM, Spencer Krum wrote:
>
> $java_home = $java_home_var
>
> Barring that, this is the correct way to use a template:
>
> $java_home = inline_template('<%= @java_home_var %>')

Well, yes, but I got the impression that the OP has a *variable name*
stored in $java_home_var and needs way to retrieve it inside a template.

Granted, such things should be done in a parser function, but the error
message is very confusing nontheless.

Brandon Metcalf

unread,
Sep 11, 2014, 10:04:57 AM9/11/14
to puppet...@googlegroups.com
That is correct.  $java_home_var does contain a variable name.  Using a variable name in a variable is almost always the wrong approach, but I couldn't find a better way to do this.  Could you explain how this could be accomplished with a parser function?
 

Henrik Lindberg

unread,
Sep 11, 2014, 1:56:50 PM9/11/14
to puppet...@googlegroups.com
On 2014-11-09 16:04, Brandon Metcalf wrote:
>
> On Sunday, September 7, 2014 9:39:56 AM UTC-5, Felix.Frank wrote:
>
> On 09/06/2014 11:09 PM, Spencer Krum wrote:
> >
> > $java_home = $java_home_var
> >
> > Barring that, this is the correct way to use a template:
> >
> > $java_home = inline_template('<%= @java_home_var %>')
>
> Well, yes, but I got the impression that the OP has a *variable name*
> stored in $java_home_var and needs way to retrieve it inside a
> template..
>
> Granted, such things should be done in a parser function, but the error
> message is very confusing nontheless.
>
>
> That is correct. $java_home_var does contain a variable name. Using a
> variable name in a variable is almost always the wrong approach, but I
> couldn't find a better way to do this. Could you explain how this could
> be accomplished with a parser function?
>
Before taking that route - where does the $java_home_var come from ?
Is it a fact? Does it refer to the name of another fact?

- henrik

> --
> 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
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/44f9aa92-0b0b-4ba4-a39b-e2fd74767457%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/44f9aa92-0b0b-4ba4-a39b-e2fd74767457%40googlegroups..com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Brandon Metcalf

unread,
Sep 11, 2014, 4:57:03 PM9/11/14
to puppet...@googlegroups.com


On Thursday, September 11, 2014 12:56:50 PM UTC-5, Henrik Lindberg wrote:
On 2014-11-09 16:04, Brandon Metcalf wrote:
>
> On Sunday, September 7, 2014 9:39:56 AM UTC-5, Felix.Frank wrote:
>
>     On 09/06/2014 11:09 PM, Spencer Krum wrote:
>      >
>      > $java_home = $java_home_var
>      >
>      > Barring that, this is the correct way to use a template:
>      >
>      > $java_home = inline_template('<%= @java_home_var %>')
>
>     Well, yes, but I got the impression that the OP has a *variable name*
>     stored in $java_home_var and needs way to retrieve it inside a
>     template..
>
>     Granted, such things should be done in a parser function, but the error
>     message is very confusing nontheless.
>
>
> That is correct.  $java_home_var does contain a variable name.  Using a
> variable name in a variable is almost always the wrong approach, but I
> couldn't find a better way to do this.  Could you explain how this could
> be accomplished with a parser function?
>
Before taking that route - where does the $java_home_var come from ?
Is it a fact? Does it refer to the name of another fact?

It looks like

$java_home_var = "${java_class}::java_home"

which is a variable in other classes depending on the value of $java_class. 

Henrik Lindberg

unread,
Sep 11, 2014, 7:07:13 PM9/11/14
to puppet...@googlegroups.com
On 2014-11-09 22:57, Brandon Metcalf wrote:
>
>
> On Thursday, September 11, 2014 12:56:50 PM UTC-5, Henrik Lindberg wrote:
>
> On 2014-11-09 16:04, Brandon Metcalf wrote:
> >
> > On Sunday, September 7, 2014 9:39:56 AM UTC-5, Felix.Frank wrote:
> >
> > On 09/06/2014 11:09 PM, Spencer Krum wrote:
> > >
> > > $java_home = $java_home_var
> > >
> > > Barring that, this is the correct way to use a template:
> > >
> > > $java_home = inline_template('<%= @java_home_var %>')
> >
> > Well, yes, but I got the impression that the OP has a
> *variable name*
> > stored in $java_home_var and needs way to retrieve it inside a
> > template..
> >
> > Granted, such things should be done in a parser function, but
> the error
> > message is very confusing nontheless.
> >
> >
> > That is correct. $java_home_var does contain a variable name..
> Using a
> > variable name in a variable is almost always the wrong approach,
> but I
> > couldn't find a better way to do this. Could you explain how
> this could
> > be accomplished with a parser function?
> >
> Before taking that route - where does the $java_home_var come from ?
> Is it a fact? Does it refer to the name of another fact?
>
>
> It looks like
>
> $java_home_var = "${java_class}::java_home"
>
> which is a variable in other classes depending on the value of $java_class.
>

I asked because the facts are available in a $facts hash, which makes it
easy to construct the name and look it up.

In Puppet 4.0, (and in 3.7.0 with future parser) you can reference
parameters via the [] operator on the Class type - e.g:

Class['class_name']['java_home']

and then no function is required.

If you are not on the latest version, and do not want to run with
--parser future, you have to write a function. Say something like this:

Puppet::Parser::Functions.newfunction(:javahome, :arity => 1,
:type => :rvalue, :doc => "The javahome does this..."") do |args|

lookup_qualified_variable(args[0], 'java_home', {} )
end

The lookup_qualified_variable is not marked as public API though, and
may not be available in older puppet versions. In which case you have to
interpolate the qualified name, and use that:

lookupvar("::#{args[0]}::java_home")

There may be a lookupvar(name) function available already in some module
if you do not want to write and maintain your own function.

(Note that functions in 3x and older versions are evaluated as methods
on the Scope class, so anything you call (like lookupvar) is a call to a
method on scope. Just saying so you know where the lookupvar and
lookup_qualified_variable comes from).

Regards
- henrik

Henrik Lindberg

unread,
Sep 11, 2014, 7:15:16 PM9/11/14
to puppet...@googlegroups.com
I should have added, that this works if java_home is a parameter of the
class only, not if it is a variable set inside of the class body - e.g.

class class_name ($java_home) {
# ...

Brandon Metcalf

unread,
Sep 12, 2014, 9:20:37 AM9/12/14
to puppet...@googlegroups.com
OK, thanks very much for your help. 
Reply all
Reply to author
Forward
0 new messages