Puppet - Escaping YAML variables for Hiera

对着背影说爱祢 提交于 2019-12-12 13:59:06

问题


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}

回答1:


With Hiera 3.3.1 you can use this in your YAML:

%%{}{EXAMPLE}

Which will output this literal:

%{EXAMPLE}



回答2:


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




回答3:


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

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



回答4:


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.



来源:https://stackoverflow.com/questions/33715620/puppet-escaping-yaml-variables-for-hiera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!