earthgecko
2012-Jul-24 15:08 UTC
[Puppet Users] erb question - can you "if val" in an "each do" statement?
Quick erb question: Can you use a if val in an each do iteration? And if so... how :/ If someone knows and is kind enough to shed some light. Trying to figure what will work and the following do NOT work. <% hosts.each do |val| %><% if val != "<%= hostname %>" %> host (<%= val %>);<% end %><% end %> <% hosts.each do |val| %><% if "<%= val %>" != "<%= hostname %>" %> host (<%= val %>);<% end %><% end %> <% hosts.each do |val| %><% if <%= val %> != "<%= hostname %>" %> host (<%= val %>);<% end %><% end %> All error with something similar to: syntax error, unexpected $undefined, expecting kTHEN or '':'' or ''\n'' or '';'' ...= hostname ; _erbout.concat "\" %> host ("; _erbout.concat... ^ .....cfg.erb:15: syntax error, unexpected kEND, expecting $end ...rbout.concat ");"; end ; end ; _erbout.concat "\n key /et... Thanks in advance.. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/wHJaXDuZXrcJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Stefan Schulte
2012-Jul-24 17:12 UTC
Re: [Puppet Users] erb question - can you "if val" in an "each do" statement?
On Tue, Jul 24, 2012 at 08:08:30AM -0700, earthgecko wrote:> Quick erb question: > > Can you use a if val in an each do iteration? And if so... how :/ If > someone knows and is kind enough to shed some light. > > Trying to figure what will work and the following do NOT work. > > <% hosts.each do |val| %><% if val != "<%= hostname %>" %> host (<%= val > %>);<% end %><% end %> > > <% hosts.each do |val| %><% if "<%= val %>" != "<%= hostname %>" %> host > (<%= val %>);<% end %><% end %> > > <% hosts.each do |val| %><% if <%= val %> != "<%= hostname %>" %> host > (<%= val %>);<% end %><% end %> > > All error with something similar to: > > syntax error, unexpected $undefined, expecting kTHEN or '':'' or ''\n'' or '';'' > ...= hostname ; _erbout.concat "\" %> host ("; _erbout.concat... > ^ > .....cfg.erb:15: syntax error, unexpected kEND, expecting $end > ...rbout.concat ");"; end ; end ; _erbout.concat "\n key /et... > > Thanks in advance..Haven''t tested it but does <% @hosts.reject { |h| h == @hostname }.each do |host| -%> <%= host %> <% end -%> work for you? -Stefan -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
earthgecko
2012-Jul-24 17:47 UTC
Re: [Puppet Users] erb question - can you "if val" in an "each do" statement?
Thank you Stefan Just testing and ... gary@mc9:/../github/dev/puppet$ erb -P -x -T ''-'' modules/csync2/templates/host.csync2.cfg.erb | ruby -c Syntax OK gary@mc9:/../github/dev/puppet$ Brilliant, thank you very much. Would still be interested to know if you can use if val in the each do context. However, for the time being problem solved, thanks for the time. Regards Gary On Tuesday, July 24, 2012 6:12:27 PM UTC+1, Stefan Schulte wrote:> > On Tue, Jul 24, 2012 at 08:08:30AM -0700, earthgecko wrote: > > Quick erb question: > > > > Can you use a if val in an each do iteration? And if so... how :/ If > > someone knows and is kind enough to shed some light. > > > > Trying to figure what will work and the following do NOT work. > > > > <% hosts.each do |val| %><% if val != "<%= hostname %>" %> host (<%= > val > > %>);<% end %><% end %> > > > > <% hosts.each do |val| %><% if "<%= val %>" != "<%= hostname %>" %> > host > > (<%= val %>);<% end %><% end %> > > > > <% hosts.each do |val| %><% if <%= val %> != "<%= hostname %>" %> host > > (<%= val %>);<% end %><% end %> > > > > All error with something similar to: > > > > syntax error, unexpected $undefined, expecting kTHEN or '':'' or ''\n'' or > '';'' > > ...= hostname ; _erbout.concat "\" %> host ("; _erbout.concat... > > ^ > > .....cfg.erb:15: syntax error, unexpected kEND, expecting $end > > ...rbout.concat ");"; end ; end ; _erbout.concat "\n key /et... > > > > Thanks in advance.. > > Haven''t tested it but does > > <% @hosts.reject { |h| h == @hostname }.each do |host| -%> > <%= host %> > <% end -%> > > work for you? > > -Stefan > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/WizWNp4VO-QJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Stefan Schulte
2012-Jul-24 21:31 UTC
Re: [Puppet Users] erb question - can you "if val" in an "each do" statement?
On Tue, Jul 24, 2012 at 10:47:40AM -0700, earthgecko wrote: [...]> Would still be interested to know if you can use if val in the each do > context. >[...]> On Tuesday, July 24, 2012 6:12:27 PM UTC+1, Stefan Schulte wrote: > > On Tue, Jul 24, 2012 at 08:08:30AM -0700, earthgecko wrote: > > > Trying to figure what will work and the following do NOT work. > > > > > > <% hosts.each do |val| %><% if val != "<%= hostname %>" %> host (<%= > > val > > > %>);<% end %><% end %>if you write <% if val != "<%= hostname %>" %> The thing between <% and %> will be interpreted as ruby code but you mix in erb syntax inside this ruby fragment. Maybe erb thinks the ruby code ends after the inner %> not after the outer %>. However what you really wanted to write was probably <% if val != @hostname %> Notice that I am addressing hostname as an instance variable. Addressing it as plain hostname (as a function) does also work but should be avoided because you may accidentally call a real ruby function instead (http://projects.puppetlabs.com/issues/14527 is a great example for that) So the endresult could be <% hosts.each do |val| -%> <% if val != @hostname -%> host (<%= val %>) <% end -%> <% end -%> -Stefan> > > > > > <% hosts.each do |val| %><% if "<%= val %>" != "<%= hostname %>" %> > > host > > > (<%= val %>);<% end %><% end %> > > > > > > <% hosts.each do |val| %><% if <%= val %> != "<%= hostname %>" %> host > > > (<%= val %>);<% end %><% end %> > > > > > > All error with something similar to: > > > > > > syntax error, unexpected $undefined, expecting kTHEN or '':'' or ''\n'' or > > '';'' > > > ...= hostname ; _erbout.concat "\" %> host ("; _erbout.concat... > > > ^ > > > .....cfg.erb:15: syntax error, unexpected kEND, expecting $end > > > ...rbout.concat ");"; end ; end ; _erbout.concat "\n key /et... > > > > > > Thanks in advance.. > > > > Haven''t tested it but does > > > > <% @hosts.reject { |h| h == @hostname }.each do |host| -%> > > <%= host %> > > <% end -%> > > > > work for you? > > > > -Stefan > > > > > > -- > You received this message because you are subscribed to the Google Groups "Puppet Users" group. > To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/WizWNp4VO-QJ. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en. >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
earthgecko
2012-Jul-25 08:39 UTC
Re: [Puppet Users] erb question - can you "if val" in an "each do" statement?
Once again Stefan, thanks. That explanation makes sense. "The thing between <% and %> will be interpreted as ruby code but you mix> in erb syntax inside this ruby fragment. >I once read somewhere that erb was really flexible. I guess not that flexible :) Regards Gary -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/byxhg0FMfnsJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.