Search code examples
escapingpuppethiera

Puppet - Escaping YAML variables for Hiera


I have a pretty simple requirement, but I've tried every escape sequence I can think of, but can't get the output needed.

I need to litterally output into a file:

%{VAR}

Here's my YAML file:

myclass::outputstuff:
    - Heres a litteral var %{VAR}
    - Heres something else %{SOMETHING}

And my template.erb:

<%= @outputstuff.each do | ostuff | -%>
<%= ostuff -%>
<% end -%>

But it like this, it outputs:

Heres a litteral var
Heres something else

If I add a percent sign like %%{VAR}, as advised by other posts, it outputs:

Heres a litteral var %
Heres something else %

If I add a backslash like %\{VAR} it outputs:

Heres a litteral var %\{VAR}
Heres something else %\{SOMETHING}

I need this lol:

Heres a litteral var %{VAR}
Heres something else %{SOMETHING}

Solution

  • The only way I managed to get this to work, was this hacky way, found here

    Basically I changed the template.erb to:

        <%- @oupputstuff.each do | ostuff | -%>
        <%- if ostuff -%>
        <%= ostuff.gsub(/___/, '%') %>
        <%- end -%>
        <%- end -%>
    

    And then in the YAML file:

    myclass::outputstuff:
        - Heres a litteral var ___{VAR}
        - Heres something else ___{SOMETHING}
    

    Very surprising none of the normal escape sequences work Lol