Accessing defined type resources from a epp template

409 views
Skip to first unread message

Gerhardus Geldenhuis

unread,
Jan 22, 2016, 11:21:21 AM1/22/16
to Puppet Users
Hi
I have an epp template that looks as follows:
<%- |$testabc, $param1, $param2| -%>
Testvar1: <%= $::testclass::testvar1 %>
Testvar2: <%= $::testclass::testvar2 %>
Param1: <%= $::testclass::param1 %>
Param2: <%= $::testclass::param2 %>
Param1: <%= $param1 %>
Param2: <%= $param2 %>
TestABC: <%= $testabc %>

and a defined type that looks as follows:

define testclass::yoyo (
  String $param1 = 'hello',
  String $param2
)
{
  file { '/tmp/defined.type':
    ensure  => file,
    content => epp('testclass/data.epp',
      {
        'param1' => $param1,
        'testabc' => 'abc',
        param2 => $param2
      } )
  }

  file { '/tmp/defined.type.erb':
    ensure  => file,
    content => template('testclass/data2.erb'),
  }

  notify { [ $param1, $param2 ]: }
}

and init.pp:
class testclass {
  $testvar1 = 'one'
  $testvar2 = ['1', 'two']
}

I call this in the following way:
include testclass
testclass::yoyo { 'one':
  param1 => 'param1 value',
  param2 => 'param2 value',
}

I can easily reference the variables from the class and get their values in the EPP template however I can't do it for the defined type without passing variables as part of the epp function call and declaring the variables at the top of the epp template. Why is there a difference in how variables are accessed from a epp template, between a defined type and a class. Am I missing something or is it by design? I am using puppet 4.3.1

Regards

jcbollinger

unread,
Jan 25, 2016, 10:00:32 AM1/25/16
to Puppet Users


On Friday, January 22, 2016 at 10:21:21 AM UTC-6, Gerhardus Geldenhuis wrote:
 
I can easily reference the variables from the class and get their values in the EPP template however I can't do it for the defined type without passing variables as part of the epp function call and declaring the variables at the top of the epp template. Why is there a difference in how variables are accessed from a epp template, between a defined type and a class. Am I missing something or is it by design? I am using puppet 4.3.1


The EPP documentation describes variable access.  In short, the behavior you observe is by design.  EPP templates get their own local scope that inherits from node scope, not from the scope in which the template is evaluated (unless that happens to be node scope).  This differs from ERB templates, whose local scope does inherit from the scope in which the template is evaluated.  As the docs describe, an EPP's local scope is much like a defined type's.

An EPP template can access any variable not visible from its own scope (such as class variables of any class) via that variable's qualified name, just like any DSL code can do.  Defined-type instances are resources, however; their local variables have no qualified names, so they are directly accessible only in the instance's local scope or a scope that inherits from it.  Defining parameters for your EPP template is the intended way for you to provide for data that vary between one evaluation and another within the same catalog-building run.


John

Reply all
Reply to author
Forward
0 new messages