puppet template syntax problem: undefined method `each' for nil:NilClass

4,799 views
Skip to first unread message

Lunixer

unread,
Aug 18, 2015, 11:16:30 AM8/18/15
to Puppet Users
I was going through the Puppet Learning doc.
Got to the template section and the ERB file, which is using the 'each' method to print the $real_servers.
The puppet run fails with pointing to the 'each' method.
Is this a bug? does anyone have any pointers?

Using:
centos 6.6
puppet 3.8.1
ruby 1.8.7

Thanks.

# cat /etc/puppet/modules/ntp/manifests/init.p
class ntp {

    # Choose platform, package name, and config file name
    case $operatingsystem {
        centos, redhat: {
            $service_name = 'ntpd'
            $conf_file = 'ntp.conf'
            $default_servers = [ "0.centos.pool.ntp.org",
                                 "1.centos.pool.ntp.org",
                                 "2.centos.pool.ntp.org", ]
        }
        debian, ubuntu: {
            $service_name = 'ntp'
            $conf_file = 'ntp.conf.debian'
            $default_servers = [ "0.debian.pool.ntp.org iburst",
                                 "1.debian.pool.ntp.org iburst",
                                 "2.debian.pool.ntp.org iburst",
                                "3.debian.pool.ntp.org iburst", ]
        }
        default: { fail("Unrecognized operating system for webserver") }
    }

    $servers_real = $default_servers

    # Install the package
    package { 'ntp':
        ensure => installed,
    }

    # Get the config file
    file { 'ntp.conf':
        path => '/etc/ntp.conf',
        ensure => file,
        require => Package['ntp'],
        content => template("ntp/${conf_file}.erb"),
    }

    # Manage the service
    service { 'ntp':
        name => $service_name,
        ensure => running,
        enable => true,
        subscribe => File['ntp.conf'],
    }

} #class



# cat /etc/puppet/modules/ntp/templates/ntp.conf.erb
<% @servers_real.each do |this_server| -%>
server <%= this_server %>
<% end -%>


#  puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template ntp/ntp.conf.erb:
  Filepath: /etc/puppet/modules/ntp/templates/ntp.conf.erb
  Line: 2
  Detail: undefined method `each' for nil:NilClass
 at /etc/puppet/modules/ntp/manifests/init.pp:26 on node puppetmaster.example.com
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

Christopher Wood

unread,
Aug 18, 2015, 12:02:27 PM8/18/15
to puppet...@googlegroups.com
Never used that, but it sounds like your instructions are using the Puppet 4 ("future") parser but your Learning VM or locally installed puppet is using the version 3 parser.

https://docs.puppetlabs.com/puppet/latest/reference/experiments_future.html

As to how to fix this, you might set your parser to "future", or you might update from yum.puppetlabs.com, or something similar.
> --
> 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 [1]puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> [2]https://groups.google.com/d/msgid/puppet-users/5d089681-9deb-4806-a3b1-4dbaa5bbe1d6%40googlegroups.com.
> For more options, visit [3]https://groups.google.com/d/optout.
>
> References
>
> Visible links
> 1. mailto:puppet-users...@googlegroups.com
> 2. https://groups.google.com/d/msgid/puppet-users/5d089681-9deb-4806-a3b1-4dbaa5bbe1d6%40googlegroups.com?utm_medium=email&utm_source=footer
> 3. https://groups.google.com/d/optout

jcbollinger

unread,
Aug 19, 2015, 9:24:06 AM8/19/15
to Puppet Users


On Tuesday, August 18, 2015 at 11:02:27 AM UTC-5, Christopher Wood wrote:
Never used that, but it sounds like your instructions are using the Puppet 4 ("future") parser but your Learning VM or locally installed puppet is using the version 3 parser.


I'm not following why you say that.  If the question were related to the parser function "each()" then it would make sense, but the question is actually about a native Ruby function (or lack thereof) in an ERB.

The error message indicates that in the template context, the variable designated by the error message has not been initialized (or has been initialized to nil).  That doesn't line up with the Puppet manifest presented, but then again, neither does the line number reported in the error message.  I am therefore inclined to think that the manifest presented is not the one that corresponds to the error message.  Also, though perhaps it's a simple typo, the apparent name of the manifest presented is "init.p", not "init.pp".  Puppet will not recognize the former.

So check your manifest.  Ensure that it is named "init.pp", that it is located in the correct directory, and that it in fact has the content presented in the question.  If the problem persists after confirming all that then it may be that an older version of the compiled manifest is cached.  In that case, restarting the puppetmaster service will not fail to clear the cache.


John

Lunixer

unread,
Aug 19, 2015, 2:31:25 PM8/19/15
to Puppet Users
The filename is actually correct.
The post above has the typo.

I had use this same module in it's simpler form as given in the learning doc and had no problems. It's only with the each part of it.
BTW, pardon my ignorance, how do you clear the puppet cache?

# ll manifests/manifests/ntp
total 8
-rw-r--r--. 1 root root 830 Aug 17 09:01 init.pp
 
 
# cat /etc/puppet/manifests/site.pp
include ntp

Lunixer

unread,
Aug 19, 2015, 6:00:56 PM8/19/15
to Puppet Users
Also, to check whether it's ruby version issue, I put the code in a small script (test.rb) to test and there is no problem at all.
This seems to be a puppet template issue. A bug, maybe?

#!/usr/bin/env ruby


$default_servers
= [ "0.debian.pool.ntp.org iburst",
                     
"1.debian.pool.ntp.org iburst",
                     
"2.debian.pool.ntp.org iburst",
                     
"3.debian.pool.ntp.org iburst", ]


$servers_real
= $default_servers

$servers_real
.each do |this_server|
  puts this_server
end

No problem executing the code outside puppet.

#./test.rb

0.debian.pool.ntp.org iburst
1.debian.pool.ntp.org iburst
2.debian.pool.ntp.org iburst
3.debian.pool.ntp.org iburst




Michael Smith

unread,
Aug 20, 2015, 12:33:03 AM8/20/15
to puppet...@googlegroups.com
I've setup a similar environment (CentOS 6.6, Ruby 1.8.7p374, Puppet 3.8.1 from gems) in Docker, and I can't reproduce the behavior. I've copied directly from your e-mail. 

I started with a single machine, so I setup
host { 'puppet':
  ensure => 'present',
  ip     => '127.0.0.1',
  target => '/etc/hosts',
}
to run `puppet agent -t`. Done without future parser (since it's not needed), and both stringify_facts=true and false (as set in POSS and PE 3.8, respectively).

Also tried `puppet apply` and setup a 2nd machine to run `puppet agent -t`.

I'm not sure what's going on except it should work.


--
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/02e9d496-b1d9-4103-ab44-740ef33172fb%40googlegroups.com.

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



--
Michael Smith
Developer, Puppet Labs

PuppetConf 2015 is coming to Portland, Oregon! Join us October 5-9.
Register now to take advantage of the Final Countdown discount save $149!

Julien Pivotto

unread,
Aug 20, 2015, 3:06:37 AM8/20/15
to puppet...@googlegroups.com
On Tue, Aug 18, 2015 at 08:16:30AM -0700, Lunixer wrote:
The error is not from the ruby version.

The template does not find @servers_real ( undefined method `each' for
nil:NilClass).

You could try to remove the file { 'ntp.conf': resource
and add a notify { $servers_real: } to check ifit is defined.

--
(o- Julien Pivotto
//\ Open-Source Consultant
V_/_ Inuits - https://www.inuits.eu
signature.asc

Lunixer

unread,
Aug 21, 2015, 12:39:03 AM8/21/15
to Puppet Users, roidel...@inuits.eu
I destroyed the VM in which I was doing this and recreated it.
Created the module again as seen in the first post and it worked. No idea what was wrong.
Thanks for the pointers.
Reply all
Reply to author
Forward
0 new messages