EPP templates and ruby code

394 views
Skip to first unread message

Martin Alfke

unread,
Sep 10, 2015, 7:34:22 AM9/10/15
to puppet-dev
Hi,

is there a way to iterate with the ruby .times function inside an EPP template?

e.g.
<% $i.times |$x| { -%>
i = <%= $x +1 %>
<% } -%>
The code above seems to need a puppet4 function with name “times”.
Result:
Error: Evaluation Error: Unknown function: 'times'.

When changing to ruby code style another error occurs:
<% $i.times do |$x| -%>
i = <%= $x +1 %>
<% end -%>
Error: Evaluation Error: Error while evaluating a Function Call, epp(): Invalid EPP: Syntax error at ‘|'

thanks,
Martin

Michael Smith

unread,
Sep 10, 2015, 11:28:31 AM9/10/15
to puppe...@googlegroups.com
Looking at the examples for the 'each' function (https://docs.puppetlabs.com/references/latest/function.html#each) you can replace 'times' with 'each' and I think it'll work.
--
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/4F22DF3E-4CD3-410E-B602-BD5B94CE089A%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Martin Alfke

unread,
Sep 11, 2015, 4:41:43 AM9/11/15
to puppe...@googlegroups.com
Hi Michael,

On 10 Sep 2015, at 17:29, Michael Smith <michae...@puppetlabs.com> wrote:

> Looking at the examples for the 'each' function (https://docs.puppetlabs.com/references/latest/function.html#each) you can replace 'times' with 'each' and I think it'll work.

each can be used on arrays. In my case the variable is an integer.
I want to print stuff $i times into the final file.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/3B71678C-05FD-4E4B-B608-EC4A769A2CDB%40puppetlabs.com.

Erik Dalén

unread,
Sep 11, 2015, 5:31:37 AM9/11/15
to puppe...@googlegroups.com
You use the range function from stdlib to create an array. For example: range(1,5).each |$x| {notice($x)}

Martin Alfke

unread,
Sep 11, 2015, 7:46:31 AM9/11/15
to puppe...@googlegroups.com
I played around a little bit with the solution mentioned by Michael:

one can use .each on an integer in a template
this results in the same behaviour of .times in ruby.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/CAAAzDLc5utqECijD-Zd%2BAUVTbbHC61hxuO%2BxNNT2ovmbzHApuw%40mail.gmail.com.

Michael Smith

unread,
Sep 11, 2015, 11:32:39 AM9/11/15
to puppe...@googlegroups.com
Yes, that's what I was getting at. I ran out of time for more context.


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



--
Michael Smith
Developer, Puppet Labs

Henrik Lindberg

unread,
Sep 12, 2015, 10:33:56 AM9/12/15
to puppe...@googlegroups.com
On 2015-11-09 11:31, Erik Dalén wrote:
> You use the range function from stdlib to create an array. For example:
> range(1,5).each |$x| {notice($x)}
>

The same is achieved with an Integer type:

Integer[1,5].each |$x| { notice $x }

It is more efficient than the range function since it does not create an
array of all values in the range.

- henrik

> On Fri, 11 Sep 2015 at 10:41 Martin Alfke <tux...@gmail.com
> <mailto:tux...@gmail.com>> wrote:
>
> Hi Michael,
>
> On 10 Sep 2015, at 17:29, Michael Smith
> <michae...@puppetlabs.com <mailto:michae...@puppetlabs.com>>
> wrote:
>
> > Looking at the examples for the 'each' function
> (https://docs.puppetlabs.com/references/latest/function.html#each)
> you can replace 'times' with 'each' and I think it'll work.
>
> each can be used on arrays. In my case the variable is an integer.
> I want to print stuff $i times into the final file.
>
> >
> > On Sep 10, 2015, at 4:34 AM, Martin Alfke <tux...@gmail.com
> <mailto:tux...@gmail.com>> wrote:
> >
> >> Hi,
> >>
> >> is there a way to iterate with the ruby .times function inside
> an EPP template?
> >>
> >> e.g.
> >> <% $i.times |$x| { -%>
> >> i = <%= $x +1 %>
> >> <% } -%>
> >> The code above seems to need a puppet4 function with name “times”.
> >> Result:
> >> Error: Evaluation Error: Unknown function: 'times'.
> >>
> >> When changing to ruby code style another error occurs:
> >> <% $i.times do |$x| -%>
> >> i = <%= $x +1 %>
> >> <% end -%>
> >> Error: Evaluation Error: Error while evaluating a Function Call,
> epp(): Invalid EPP: Syntax error at ‘|'
> >>
> >> thanks,
> >> Martin
> >>
> >> --
> >> 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
> <mailto:puppet-dev%2Bunsu...@googlegroups.com>.
> >> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-dev/4F22DF3E-4CD3-410E-B602-BD5B94CE089A%40gmail.com.
> >> For more options, visit https://groups.google.com/d/optout.
> >
> > --
> > 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
> <mailto:puppet-dev%2Bunsu...@googlegroups.com>.
> > To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-dev/3B71678C-05FD-4E4B-B608-EC4A769A2CDB%40puppetlabs.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> 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
> <mailto:puppet-dev%2Bunsu...@googlegroups.com>.
> --
> 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
> <mailto:puppet-dev+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-dev/CAAAzDLc5utqECijD-Zd%2BAUVTbbHC61hxuO%2BxNNT2ovmbzHApuw%40mail.gmail.com
> <https://groups.google.com/d/msgid/puppet-dev/CAAAzDLc5utqECijD-Zd%2BAUVTbbHC61hxuO%2BxNNT2ovmbzHApuw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

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

Reply all
Reply to author
Forward
0 new messages