Just Lucky Really
Just Lucky Really

Reputation: 1401

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}

Upvotes: 4

Views: 5656

Answers (4)

bryan kennedy
bryan kennedy

Reputation: 7199

With Hiera 3.3.1 you can use this in your YAML:

%%{}{EXAMPLE}

Which will output this literal:

%{EXAMPLE}

Upvotes: 6

Rick Fletcher
Rick Fletcher

Reputation: 538

I needed a way to represent the literal %{VAR} string that worked across both Hiera 1.x and 2.x, so that nothing broke while I was mid-upgrade.

Here's the dumb trick I came up with, which worked for me in both 1.3.4 and 2.0.0:

---
"%": "%"

myclass::outputstuff:
  - "Heres a literal var %{hiera('%')}{VAR}"

After interpolation -- in either Hiera version -- this evaluates to:

{"%":"%","myclass::outputstuff":["Heres a literal var %{VAR}"]}

Of course this results in that extra % key floating around, but that doesn't do any harm. Then, post-upgrade I can switch to %{literal('%')}, and remove the extra YAML entry.

Upvotes: 0

Chamila Chulatunga
Chamila Chulatunga

Reputation: 4914

From v2.0.0 on, there is the literal function, which is the more 'proper' way to do it:

%{literal('%')}{VAR}

Upvotes: 2

Just Lucky Really
Just Lucky Really

Reputation: 1401

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

Upvotes: 1

Related Questions