在ruby模板中进行深层hiera查找仅返回最后一个结果

问题描述 投票:0回答:1

我具有以下等级:

profile::example::firewalls:
  - serverclass1:
    label: http
    port: 80
    label: https
    port: 443
  - serverclass2:
    label: ssh
    port: 22
    label: telnet
    port: 21

我尝试以以下方式在erb模板中调用它:

<% @example = scope().call_function('hiera',['profile::example::firewalls']) -%>
show me the array!
<%= @example %>

奇怪的是,它返回以下内容:

+show me the array!
+[{"serverclass1"=>nil, "label"=>"https", "port"=>443}, {"serverclass2"=>nil, "label"=>"telnet", "port"=>21}]

一旦我有了一个完整的数组,我最终将在ruby的for / each循环内使用它,但就目前而言,这似乎只返回最终结果,而不是我期望的所有结果。

puppet erb hiera
1个回答
0
投票

问题始于您的YAML数据结构,其中包含重复的“标签”键。这就是为什么您的某些数据在输出中丢失的原因。

((有关更多信息,请参见this相关的堆栈溢出答案。]

尽管还不清楚您要做什么,但在我看来,使用这样的YAML数据结构会更好:

profile::example::firewalls:
  serverclass1:
    http: 80
    https: 443
  serverclass2:
    ssh: 22
    telnet: 21

然后您可以使用类似以下代码的方法在ERB模板中进行迭代:

<% scope().call_function('lookup',['profile::example::firewalls']).each do |server_class, data| -%>
Server class: <%= server_class %>
  <%- data.each do |key, value| -%>
  <%= key %> --- <%= value %>
  <%- end -%>
<% end -%>

还请注意,我删除了不推荐使用的hiera()函数,并将其替换为lookup()

© www.soinside.com 2019 - 2024. All rights reserved.