On Wednesday, August 12, 2015 at 8:52:50 AM UTC-5, b.gold...@gmail.com wrote: > > Hi Guys > I am new to puppet and ruby. I am working on some erb template for ldap. > > Following is sample code > > > <% if TCPSocket.new("host1", 636) and TCPSocket.new("host2", 636) %> > host xxxx > port 636 > tls_checkpeer yes > <% else -%> > host yyyy > port 389 > tls_checkpeer no > <% end %> > > > Above works if 636 is open but if its not then it never goes to else but > raise exception of connection refused and eventually puppet fails. > I tried catching exception in erb template but it didnt work. > > Possible to catch exceptions like below ? I want to exit it if any > exception is raised. > > rescue Errno:: ECONNREFUSED => e > >
First of all, I urge you to choose an altogether different way to determine which alternative you want your template to exercise. It is usually better in Puppet to choose configuration details based on static information (recorded in an Hiera data store, for example) or semi-static information (e.g. based on resources exported during the process of building other nodes' catalogs). There is a range, but ephemeral data such as whether the master can establish a TCP connection to a particular host at a particular time are a particularly poor basis for such choices. If you insist on proceeding in that direction, however, then you need to recognize that when TCPSocket.new() fails, it throws an exception rather than returning a value that evaluates to false. If you want processing to continue within your template, then you need it to catch that exception. That would look something like this: <% begin TCPSocket.new("host1", 636) and TCPSocket.new("host2", 636) %> host xxxx port 636 tls_checkpeer yes <% rescue -%> host yyyy port 389 tls_checkpeer no <% end %> Overall, always remember that ERB is just a somewhat unusual way to write ordinary Ruby code. It's expressed inside out, but once you learn to see through that to the all-Ruby code it represents, you can rely on your Ruby knowledge (or someone else's) to understand how to create, interpret, and use ERB templates. John -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/7655cf9e-07cf-45d4-b8b0-b18b6b7b61fb%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.