Error with EPP template from "Learning Puppet 4"

593 views
Skip to first unread message

David Karr

unread,
May 19, 2016, 4:57:45 PM5/19/16
to Puppet Users
I'm stepping through "Learning Puppet 4", and I ran into an error following steps in the book, and I want to understand what went wrong before I report it.

The section in question is "Using Puppet EPP Templates" in chapter 13.

I'll work backwards from the error I'm getting, showing the relevant files afterwards.  Here is what I see when I apply the manifest:
[vagrant@client puppet]$ sudo puppet apply --environment test manifests/
Warning: Unknown variable: '::puppet::common_loglevel'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:3:21
Warning: Unknown variable: 'puppet::agent_loglevel'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:7:7
Warning: Unknown variable: '::puppet::server'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:10:18
Warning: Unknown variable: 'puppet::apply_loglevel'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:14:7
Notice: Compiled catalog for client.example.com in environment test in 0.04 seconds
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/content: content changed '{md5}a72aadac19feefd06f10fb6b8f90c5f4' to '{md5}0f89a3d91b54aec8463b4a250a493f36'
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/owner: owner changed 'vagrant' to 'root'
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/group: group changed 'vagrant' to 'wheel'
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/mode: mode changed '0664' to '0644'
Notice: Applied catalog in 0.06 seconds
[vagrant@client puppet]$

The template in question, right from the book, is this:
# Generated by Puppet EPP template processor
[master]
   
log_level = <%= $::puppet::common_loglevel %>

# This is used by "puppet agent"
[agent]
<% if $puppet::agent_loglevel != undef { -%>
    log_level = <%= $::puppet::agent_loglevel %>
<% } -%>
    server = <%= $::puppet::server %>

# This is used for "puppet apply"
[user]
<% if $puppet::apply_loglevel != undef { -%>
    log_level = <%= $::puppet::apply_loglevel %>
<% } -%>

Likely the most important piece to see now is the part of the "init.pp" manifest that declares the parameters being referenced here:
class puppet(
 
$version         = 'latest',
 
$status          = 'running',
 
$enabled         = true,
 
$server          = 'puppet.example.com',
 
$common_loglevel = 'warning',
 
$agent_loglevel  = undef,
 
$apply_loglevel  = undef,

) {

I imagine the syntax of the variable references in the EPP file have to be slightly different, but I have no idea what it should be.

I also find it curious that the error messages print the name of the variable differently for different messages, sometimes prefixed with "::" and sometimes not.  Any background that would explain that?

Peter Huene

unread,
May 19, 2016, 5:16:17 PM5/19/16
to puppet...@googlegroups.com
Hi David:

On Thu, May 19, 2016 at 1:57 PM, David Karr <davidmic...@gmail.com> wrote:
I'm stepping through "Learning Puppet 4", and I ran into an error following steps in the book, and I want to understand what went wrong before I report it.

The section in question is "Using Puppet EPP Templates" in chapter 13.

I'll work backwards from the error I'm getting, showing the relevant files afterwards.  Here is what I see when I apply the manifest:
[vagrant@client puppet]$ sudo puppet apply --environment test manifests/
Warning: Unknown variable: '::puppet::common_loglevel'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:3:21
Warning: Unknown variable: 'puppet::agent_loglevel'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:7:7
Warning: Unknown variable: '::puppet::server'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:10:18
Warning: Unknown variable: 'puppet::apply_loglevel'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:14:7
Notice: Compiled catalog for client.example.com in environment test in 0.04 seconds
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/content: content changed '{md5}a72aadac19feefd06f10fb6b8f90c5f4' to '{md5}0f89a3d91b54aec8463b4a250a493f36'
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/owner: owner changed 'vagrant' to 'root'
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/group: group changed 'vagrant' to 'wheel'
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/mode: mode changed '0664' to '0644'
Notice: Applied catalog in 0.06 seconds
[vagrant@client puppet]$


Based on the warnings, it looks like the "puppet" class hasn't been declared.  Did you `include puppet` somewhere or otherwise classify the node to include the "puppet" class? 
The EPP is fine; the code inside an EPP <% %> is expected to be valid Puppet code.
 

I also find it curious that the error messages print the name of the variable differently for different messages, sometimes prefixed with "::" and sometimes not.  Any background that would explain that?

When a variable name has a leading "::" it forces a lookup in the top-scope.  An example where it would be needed is when you have a top-scoped variable and a local variable of the same name; a leading "::" would ensure that the top-scope variable is used and not the local variable.

For what it's worth, here's what a warning from a prototype Puppet compiler I'm working on looks like when evaluating your example (hopefully it's a bit more clear):

Warning: <epp>:3:21: could not look up variable $::puppet::common_loglevel because class 'puppet' has not been declared.
  log_level = <%= $::puppet::common_loglevel %>
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~
 ...

Note: the filename is "<epp>" because I stuck your EPP source in an "inline_epp" call rather than a file.


--
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/01a83a7f-c3f2-4ca6-83d9-df09bec41527%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Karr

unread,
May 19, 2016, 5:23:37 PM5/19/16
to Puppet Users
On Thursday, May 19, 2016 at 2:16:17 PM UTC-7, Peter Huene wrote:
Hi David:

On Thu, May 19, 2016 at 1:57 PM, David Karr <davidmic...@gmail.com> wrote:
I'm stepping through "Learning Puppet 4", and I ran into an error following steps in the book, and I want to understand what went wrong before I report it.

The section in question is "Using Puppet EPP Templates" in chapter 13.

I'll work backwards from the error I'm getting, showing the relevant files afterwards.  Here is what I see when I apply the manifest:
[vagrant@client puppet]$ sudo puppet apply --environment test manifests/
Warning: Unknown variable: '::puppet::common_loglevel'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:3:21
Warning: Unknown variable: 'puppet::agent_loglevel'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:7:7
Warning: Unknown variable: '::puppet::server'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:10:18
Warning: Unknown variable: 'puppet::apply_loglevel'. at /etc/puppetlabs/code/environments/test/modules/puppet/templates/puppet.conf.epp:14:7
Notice: Compiled catalog for client.example.com in environment test in 0.04 seconds
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/content: content changed '{md5}a72aadac19feefd06f10fb6b8f90c5f4' to '{md5}0f89a3d91b54aec8463b4a250a493f36'
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/owner: owner changed 'vagrant' to 'root'
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/group: group changed 'vagrant' to 'wheel'
Notice: /Stage[main]/Main/File[/etc/puppetlabs/puppet/puppet.conf]/mode: mode changed '0664' to '0644'
Notice: Applied catalog in 0.06 seconds
[vagrant@client puppet]$


Based on the warnings, it looks like the "puppet" class hasn't been declared.  Did you `include puppet` somewhere or otherwise classify the node to include the "puppet" class? 

The only file in the "manifests" directory is "init.pp", which is this:
class puppet(
 
# input parameters and default values for the class

  $version        
= 'latest',
  $status          
= 'running',
  $enabled        
= true,
  $server          
= 'puppet.example.com',
  $common_loglevel
= 'warning',
  $agent_loglevel  
= undef,
  $apply_loglevel  
= undef,
) {


 
# echo the input provided
  notice
("Install the $version version of Puppet, ensure it's $status, and set boot time start $enabled.")

 
# install puppet-agent
 
package { 'puppet-agent':
   
ensure => 'latest',
    notify
=> Service['puppet'],
 
}

 
# manage the puppet service
  service
{ 'puppet':
   
ensure => 'running',
    enable
=> true,
    subscribe
=> Package['puppet-agent'],
 
}
}

class puppet::agent {
}

file
{ '/etc/puppetlabs/puppet/puppet.conf':
 
ensure => file,
  owner
=> 'root',
 
group => 'wheel',
  mode  
=> '0644',
#  source => 'puppet:///modules/puppet/puppet.conf',
  content
=> epp('puppet/puppet.conf.epp'),
}

 

Perhaps I wasn't clear.  All the variable references in the template use the same syntax, using the "$::puppet" prefix, yet half of the error messages refer to the "::" prefix, and half of them do not.

Peter Huene

unread,
May 19, 2016, 5:36:53 PM5/19/16
to puppet...@googlegroups.com
Without including the puppet class, the evaluation of the epp function here will result in those "unknown variable" warnings and they will evaluate to undef (or cause an error in more recent versions of Puppet with strict variable checking enabled). 
The compiler emits the warning based on the variable's name as it was specified in the source.  The EPP template you've pasted contains two variable references, $puppet::agent_loglevel and $puppet::apply_loglevel, in the "if" conditionals that are not prefixed with "::", which is why you do not see the leading "::" in the warnings.

The places where these two variables are actually prefixed with "::" are never evaluated because the variables evaluate to undef and the conditionals evaluate to false.


--
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.

David Karr

unread,
May 19, 2016, 5:52:10 PM5/19/16
to Puppet Users

Ok, so it appears that if I add "include puppet" right after the body of the "puppet" class, it processes the template without undefined variable references.  That just seems odd.  Is that really what I need to do?
 

Yeah, sorry.  I missed that.  Now that I have the class properly included (still seems odd that I have to declare it and also "include" it), I see that the template processing produces the same result for all of these references with or without the "::" prefix.  From your explanation, I see that that's just because there are no potential conflicts.

Peter Huene

unread,
May 19, 2016, 6:11:48 PM5/19/16
to puppet...@googlegroups.com
There are two parts to using a class in Puppet: defining the class and declaring the class.

Defining a class is what you were originally doing (i.e. "class puppet(...) { ... }"); this lets Puppet's compiler know that there is a class named "puppet" so that it can later be declared, usually based on some conditional logic specific to the node's role.

Declaring a class (one possible way is by calling the "include" function) causes the compiler to evaluate the class' parameters into its scope (something your EPP was relying on) and evaluates the class' body, in this case adding the package and service resources to the resource catalog.  Typically class declaration is performed through a node classifier, Hiera data, or node definition statements in your site.pp.

I'm not the best explainer, so perhaps the docs can be of more help: https://docs.puppet.com/puppet/latest/reference/lang_classes.html
 

--
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.
Reply all
Reply to author
Forward
0 new messages