Help on puppet ERB template

126 views
Skip to first unread message

b.gold...@gmail.com

unread,
Aug 12, 2015, 9:52:50 AM8/12/15
to Puppet Users
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



Thanks for help





jcbollinger

unread,
Aug 13, 2015, 10:08:01 AM8/13/15
to Puppet Users
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

Reply all
Reply to author
Forward
0 new messages