"Each" loop issue

90 views
Skip to first unread message

martin...@gmail.com

unread,
Jun 30, 2014, 5:58:19 PM6/30/14
to puppet...@googlegroups.com
Hi Puppet users!

I'm having an issue with "each loop" on puppet, and I just can't figure out what am I doing wrong, so any help will be much appreciated.

Here is my test.pp example:

class mytestclass {
  $stuff = ["1", "2", "3"]

  each($stuff) |$x| {
    notice { $x: }
  }
}

And here is the output when trying to apply it: "Error: Could not parse for environment production: Could not match |$x| at /tmp/test.pp:10"

I've "parser = future" inside my puppet.conf, ruby-rgen is installed in both client and server. I'm also running it with "--parser future" on the client, but no joy.
I've  been playing with variants like "$stuff.each", using hashes, and a few more desperate ideas.

OS: Ubuntu 12.04
Puppet: 3.5.1-1puppetlabs1
ruby-rgen: 0.6.5-1puppetlabs1


I'm clearly missing something, but can't find what.

Any suggestion is welcomed!

Cheers,

Martin

Tristan Smith

unread,
Jun 30, 2014, 6:57:26 PM6/30/14
to puppet...@googlegroups.com
Hayo.

Other than s/notice/notify/ for best effect, that works a treat when I invoke it with puppet apply --parser future test.pp (added an 'include mytestclass' for invocation, but that's it).

The parser=future is required on whatever node is doing the catalog compilation, and needs to go in [main] for best effect, I believe (certainly won't do anything in [agent], might be sufficient in [master])

Henrik Lindberg

unread,
Jul 1, 2014, 11:27:16 AM7/1/14
to puppet...@googlegroups.com
On 2014-30-06 23:58, martin...@gmail.com wrote:
> Hi Puppet users!
>
> I'm having an issue with "each loop" on puppet, and I just can't figure
> out what am I doing wrong, so any help will be much appreciated.
>
> Here is my test.pp example:
>
> class mytestclass {
> $stuff = ["1", "2", "3"]
>
> each($stuff) |$x| {
> notice { $x: }

You probably meant

notify { $x: }

notice is a logging function, and you can do

notice $x

> }
> }

For this snippet to work, you must also do

include mytestclass

outside the scope of that class, otherwise nothing happens.

>
> And here is the output when trying to apply it: /"Error: Could not parse
> for environment production: Could not match |$x| at /tmp/test.pp:10"/
>
Sounds like --parser future is not in effect.

> I've "parser = future" inside my puppet.conf, ruby-rgen is installed in
> both client and server. I'm also running it with "--parser future" on
> the client, but no joy.
> I've been playing with variants like "$stuff.each", using hashes, and a
> few more desperate ideas.
>
> OS: Ubuntu 12.04
> Puppet: 3.5.1-1puppetlabs1
> ruby-rgen: 0.6.5-1puppetlabs1
>
>
> I'm clearly missing something, but can't find what.
>

As another poster suggested, maybe you have the parser=future setting in
the wrong section.

Hope the above helps

- henrik

--

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

martin...@gmail.com

unread,
Jul 1, 2014, 8:25:15 PM7/1/14
to puppet...@googlegroups.com
Henrik, Tristan, thanks for your replies!

I've added "parser = future" on both master and agent, inside [main], and also I'm still using "--parser future", just in case.
You both were right, and I'd use "notify" instead of "notice" in that snippet.

I think that parser option is working now, but it's breaking a template that was working until now. So here I go again for your help, as I don't think that the parser should be doing this.

Here is the output of the new error:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template sudo/sudoers.erb:
  Filepath: /etc/puppet/environments/integration/modules/sudo/templates/sudoers.erb
  Line: 42
  Detail: undefined method `each' for nil:NilClass
 on node test01

Here is the line 42 of the template file:

....<snip>...
DEPLOY ALL = NOPASSWD: /usr/bin/service apache2 restart
DEPLOY ALL = NOPASSWD: /usr/bin/service php5-fpm restart
<% @server_roles.each do |role| -%>              <====== line 42
<% if role == "testuser" %>
DEPLOY ALL = NOPASSWD: /usr/bin/service tomcat7 restart
DEPLOY ALL = NOPASSWD: /usr/bin/service tomcat7 start
DEPLOY ALL = NOPASSWD: /usr/bin/service tomcat7 stop
<% end %>
...<snip>....

I've tried with "each @server_roles" too, but still broken.
May be enabling the extra features of the parser, puppet tries to do something else with the word "each", but I'm totally lost here.

Is this behavior expected? Is there any way that I can debug any deeper on this?

Again, thanks in advance for any help.

Cheers,

Martin

Ellison Marks

unread,
Jul 3, 2014, 12:57:46 PM7/3/14
to puppet...@googlegroups.com
That error means that the @server_roles variable is holding nil, as nil does not have an each method to call. Perhaps the new parser is evaluating things differently and not setting that variable.

martin...@gmail.com

unread,
Jul 3, 2014, 5:00:52 PM7/3/14
to puppet...@googlegroups.com
Hi Ellison,

That's right. That seems to be the case.
I've been googling around, and seems that the new parser has a different scope from where it can grab the variables. Sadly, I can't find the way to make it work. 
Should I include something special in order to make the hiera variable available to the template?

Again, thanks for taking the time.

Best regards,

Henrik Lindberg

unread,
Jul 3, 2014, 10:02:14 PM7/3/14
to puppet...@googlegroups.com
On 2014-03-07 23:00, martin...@gmail.com wrote:
> Hi Ellison,
>
> That's right. That seems to be the case.
> I've been googling around, and seems that the new parser has a different
> scope from where it can grab the variables. Sadly, I can't find the way
> to make it work.
> Should I include something special in order to make the hiera variable
> available to the template?
>

There was a change to dynamic scoping - they were allowed when they
should not. That problem was fixed in the future parser branch of the
logic. This issue is PUP-1220.

Not seeing your entire configuration it is hard to tell where the
missing variable is defined and if it should be visible or not.
If you can reference the variable at the point where you are making the
call to produce the template text, then something odd is going on.

say doing something like:

notice "The variable is set to: $the_var"
template(...)

Martin

unread,
Jul 4, 2014, 9:00:05 AM7/4/14
to puppet...@googlegroups.com
Henrik,

Thank you for such clear reply!. I'll start debugging to find out what I'm missing here. 

I want to thank you for your support to the Puppet community, developing or taking the time to answer here in the list, your participation is really appreciated.
In the other side, I think that documentation for the new parser is a bit poor. I've found more information googling around than in the official doc, and may be some questions in the list (as mine for ex) can be avoided with some improvements in the doc site. Also some troubleshooting guide would be awesome.

Thanks again for all your hard work!

Cheers,

Martin


 


--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/ABTTFbVfr48/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/lp51u5%24elo%241%40ger.gmane.org.

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

Henrik Lindberg

unread,
Jul 4, 2014, 9:53:27 AM7/4/14
to puppet...@googlegroups.com
On 2014-04-07 14:59, Martin wrote:
> Henrik,
>
> Thank you for such clear reply!.. I'll start debugging to find out what
> I'm missing here.
>
> I want to thank you for your support to the Puppet community, developing
> or taking the time to answer here in the list, your participation is
> really appreciated.
> In the other side, I think that documentation for the new parser is a
> bit poor. I've found more information googling around than in the
> official doc, and may be some questions in the list (as mine for ex) can
> be avoided with some improvements in the doc site.

There will be new documentation for the new parser when it is released,
as Puppet 4.0.0.

We have also been busy writing a specification of the language:
https://github.com/puppetlabs/puppet-specifications and we recently made
this w.i.p public. Please note that the specification is not finalized
yet so it cannot yet be considered authoritative. Also note that the
spec is not as user friendly as the regular documentation.

> Also some
> troubleshooting guide would be awesome.
>
Yeah, I would have loved to have had one - but for something like that
to be comprehensive it would be daunting to produce. Have you checked
out https://ask.puppetlabs.com/questions/ ?

Martin

unread,
Jul 4, 2014, 10:16:00 AM7/4/14
to puppet...@googlegroups.com
Henrik,

Didn't know about "Ask". I've added to the bookmarks for the next time ;)

Thanks!


--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/ABTTFbVfr48/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages