Accessing variables from templates in wrapped defines

43 views
Skip to first unread message

Martin Alfke

unread,
Aug 11, 2015, 3:38:05 AM8/11/15
to puppet-dev
Hi,

I have an issue with templates and variable lookup when used in wrapped defines - which was working in older puppet versions (maybe due to the dynamic variable lookup):

e.g. The following code:

The first define setting a local variable and calling a second define with a parameter:

cat define1/manifests/init.pp
define define1 {
$var = 'test'
define2 { “define1_${name}":
template => 'define1/test.erb',
}
}

The second define using a resource type and makes use of the parameter:

cat define2/manifests/init.pp
define define2 ($template) {
notify { "define2_${name}":
message => template($template),
}
}

The template which should be used in the second define (handed over as parameter):

cat define1/templates/test.erb
test = <%= @var %>

The result:

puppet apply -e 'define1 { 'test': }'
Notice: Compiled catalog for puppetmaster.example.net in environment production in 0.33 seconds
Notice: test =

Notice: /Stage[main]/Main/Define1[test]/Define2[define1_test]/Notify[define2_define1_test]/message: defined 'message' as 'test =
'
Notice: Applied catalog in 0.03 seconds


If the first define would be a class then I could do a scope.lookupvar.
If I change the template to do scope.lookupvar I receive the following:

puppet apply -e 'define1 { 'test': }'
Warning: Scope(Define2[define1_test]): Could not look up qualified variable 'define1::var'; class define1 could not be found
Notice: Compiled catalog for puppetmaster.example.net in environment production in 0.32 seconds
Notice: test =

Notice: /Stage[main]/Main/Define1[test]/Define2[define1_test]/Notify[define2_define1_test]/message: defined 'message' as 'test =
'
Notice: Applied catalog in 0.03 seconds

How can I access the variable from the first define?
I can change the second define to have another parameter so it knows about the template variable.
Is there any other solution available?

Thanks,
Martin

R.I.Pienaar

unread,
Aug 11, 2015, 3:39:53 AM8/11/15
to puppet-dev
changing the 2nd define is the best solution, this already didn't work inside .pp files
for a long time and thankfully now it also doesnt work in templates

Ryan Whitehurst

unread,
Aug 11, 2015, 1:39:49 PM8/11/15
to puppe...@googlegroups.com

Martin Alfke writes:

> How can I access the variable from the first define? I can
> change the second define to have another parameter so it knows
> about the template variable. Is there any other solution
> available?

It's not exactly the same, but this works now, using EPP instead
of ERB:

scope_epp/manifests/define1.pp

~~~
define scope_epp::define1 ($var = 'test') {
scope_epp::define2 { $name:
epp => 'scope_epp/test.epp',
}
}
~~~ scope_epp/manifests/define2.pp

~~~
define scope_epp::define2 ($epp) {
notify { $name:
message => epp($epp, {'name' => $name}),
}
}
~~~ scope_epp/templates/test.epp

~~~ <%= Scope_epp::Define1[$name]['var'] %> ~~~

Then running:

% /opt/puppetlabs/bin/puppet apply -e 'scope_epp::define1 {
"thing": }' --modulepath=. Notice: Compiled catalog for localhost
in environment production in 0.64 seconds Notice: test Notice:
/Stage[main]/Main/Scope_epp::Define1[thing]/Scope_epp::Define2[thing]/Notify[thing]/message:
defined 'message' as 'test' You can't use that to access
variables from the defined type, but you can access parameters.
Now whether you should do that or not...

Martin Alfke

unread,
Aug 12, 2015, 3:01:01 AM8/12/15
to puppe...@googlegroups.com
Hi Ryan,
On 11 Aug 2015, at 19:22, Ryan Whitehurst <r...@puppetlabs.com> wrote:

>
> Martin Alfke writes:
>
>> How can I access the variable from the first define? I can change the second define to have another parameter so it knows about the template variable. Is there any other solution available?
> It's not exactly the same, but this works now, using EPP instead of ERB:

Many thanks for pointing to EPP.
We can not yet switch to EPP.
We want our example42 modules to be compatible with puppet 3 and 4 and will do a version bump very soon offering new language features..

Besides this:
when I saw this construct (accessing variables from another module(!! sic)) my in-brain puppet parser already went nuts.
I decided to add the variable as parameter which makes it now far more clear where the value is taken from.

Best,
Martin


> scope_epp/manifests/define1.pp
>
> ~~~
> define scope_epp::define1 ($var = 'test') { scope_epp::define2 { $name: epp => 'scope_epp/test.epp', } }
> ~~~ scope_epp/manifests/define2.pp
>
> ~~~
> define scope_epp::define2 ($epp) { notify { $name: message => epp($epp, {'name' => $name}), } }
> ~~~ scope_epp/templates/test.epp
>
> ~~~ <%= Scope_epp::Define1[$name]['var'] %> ~~~
>
> Then running:
>
> % /opt/puppetlabs/bin/puppet apply -e 'scope_epp::define1 { "thing": }' --modulepath=. Notice: Compiled catalog for localhost in environment production in 0.64 seconds Notice: test Notice: /Stage[main]/Main/Scope_epp::Define1[thing]/Scope_epp::Define2[thing]/Notify[thing]/message: defined 'message' as 'test' You can't use that to access variables from the defined type, but you can access parameters. Now whether you should do that or not…

I prefer to not do that.
;-)

>
> --
> You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/87mvxxspjc.fsf%40rafiki.corp.puppetlabs.net.
> For more options, visit https://groups.google.com/d/optout.

Felix Frank

unread,
Aug 12, 2015, 8:05:50 AM8/12/15
to puppe...@googlegroups.com
Oh my, you were relying on actual buggy behavior.

https://projects.puppetlabs.com/issues/22800

Ah, the memories... :-)

Cheers,
Felix

John Bollinger

unread,
Aug 12, 2015, 4:50:00 PM8/12/15
to Puppet Developers


On Tuesday, August 11, 2015 at 12:39:49 PM UTC-5, Ryan Whitehurst wrote:

Martin Alfke writes:

> How can I access the variable from the first define? I can
> change the second define to have another parameter so it knows
> about the template variable. Is there any other solution
> available?
 
It's not exactly the same, but this works now, using EPP instead
of ERB:



Shocking.

To the best of my understanding, it should not work in EPP any more than it does in ERB or Puppet DSL.  If it does work in EPP then that would be a bug, and it would be unwise to rely on that bug to remain unfixed.


John

Ryan Whitehurst

unread,
Aug 12, 2015, 8:06:56 PM8/12/15
to puppe...@googlegroups.com

John Bollinger writes:

> Shocking.
>
> To the best of my understanding, it should not work in EPP any
> more than it does in ERB or Puppet DSL. If it does work in EPP
> then that would be a bug, and it would be unwise to rely on that
> bug to remain unfixed.

Note that I was referring to accessing resource attributes, which
is a feature of Puppet 4:

https://docs.puppetlabs.com/puppet/4.2/reference/lang_data_resource_reference.html#accessing-attribute-values
Reply all
Reply to author
Forward
0 new messages