puppetpuppet-enterprise

Puppet epp conditionals is there elsif?


Q: does puppet epp template syntax support elsif conditionals?

I am receiving the following error when building a template with puppet epp:

Evaluation Error: Error while evaluating a Function Call, epp(): Invalid EPP: Syntax error at 'elsif'

It seems like puppet syntax should support the use of elsif here. I've looked around and can't seem to see much on this topic.


My template:

<% if $facts[env] == test  { %> foo:
    <% if $facts[site] == uk { %> bar uk: <% } %>
    <% elsif $facts[site] == fr { %> bar fr: <% } %>
    <% else $facts[site] == us { %> bar us: <% } %>
<% } %>

Latest Edits:

Along with John Bollinger's helpful comments below, there shouldn't be any conditions outside of the else. If there are, Puppet throws the misleading error message above. So for my use-case the below works well:

 <%
  if $facts[env] == test  {
    %> foo:
    <%
    if $facts[site] == uk    {
      %> bar uk: <%
    } elsif $facts[site] == fr {
      %> bar fr: <%
    } else {
      %> bar us: <%
    }
  }
 %>

Solution

  • The problem is that there is template text (albeit all whitespace) between the closing brace of the if block and the elseif. From the perspective of the Puppet code, template text is effectively an output statement, and that cannot appear between an } and an elsif.

    Since it seems unlikely that you actually want the particular whitespace that is causing the issue, I think you could resolve it by suppressing it:

    <% if $facts[env] == test  { %> foo:
        <% if $facts[site] == uk { %> bar uk: <% } -%>
        <%- elsif $facts[site] == fr { %> bar fr: <% } -%>
        <%- else $facts[site] == us { %> bar us: <% } -%>
    <% } %>
    

    That leaves the linebreak and whitespace after foo:, but suppresses the whitespace between the if and elseif, between the elseif and else, and following the else.

    Alternatively, I usually find template code easier to read when it has fewer tags. Like this, maybe:

    <%
      if $facts[env] == test  {
        %> foo:
        <%
        if $facts[site] == uk    {
          %> bar uk: <%
        } elsif $facts[site] == fr {
          %> bar fr: <%
        } else $facts[site] == us  {
          %> bar us: <%
        }
      }
     %>