将哈希作为json在puppet epp模板中写

发布于 2025-02-08 03:35:58 字数 890 浏览 1 评论 0原文

我面对从木偶中的.epp模板打印值时

  1. ,我在hiera中有以下数据,该数据定义了组(hashes的数组)
profiles::groups:
  - name: 'admins'
    type: 1
    rights: []
  - name: 'users'
    type: 2
    rights: []
  1. 上面的hiera键在.epp模板中称为上述
   .....    
   groups = <%= require 'json'; JSON.pretty_generate scope['groups'] %> 
   .....
  1. 声明中的。 (从上方的EPP模板),但
....... 
groups = [
  {
    "name": "admins",
    "type": 1,
    "rights": []
  },
  {
    "name": "users",
    "type": 2,
    "rights": []
  }
]
.....
Error Message: Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, epp(): Invalid EPP: Syntax error at 'json' 

在.erb模板中声明相同格式的同时,给出了错误,并产生所需的输出。

I am facing while printing values from .epp template in puppet

  1. I have following data in hiera, which defines groups (array of hashes)
profiles::groups:
  - name: 'admins'
    type: 1
    rights: []
  - name: 'users'
    type: 2
    rights: []
  1. The above hiera key is called in .epp template like this
   .....    
   groups = <%= require 'json'; JSON.pretty_generate scope['groups'] %> 
   .....
  1. Above declaration should print the values in file in following format (from above epp template) but gives error as mentioned below
....... 
groups = [
  {
    "name": "admins",
    "type": 1,
    "rights": []
  },
  {
    "name": "users",
    "type": 2,
    "rights": []
  }
]
.....
Error Message: Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, epp(): Invalid EPP: Syntax error at 'json' 

While declaring the same format in .erb template works fine and it produces desired output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生池水覆流年 2025-02-15 03:35:59

在ERB中,它是嵌入式Ruby,您可以使用常规红宝石功能,在EPP上是嵌入式木偶,因此您需要使用木偶功能。 There is a to_json_pretty available in stdlib https://forge.puppet.com/modules/puppetlabs/stdlib 将完成这项工作,您可以这样使用;

groups = <%= $groups.to_json_pretty %> 

我刚刚对其进行了测试;

# test/manifests/jsontest.pp
class test::jsontest {

  $groups = lookup('test::groups')

  file { '/root/epp':
    ensure  => file,
    content => epp("test/jsondata.epp", {groups => $groups})
  }
}
# test/templates/jsondata.epp
groups = <%= $groups.to_json_pretty %> 
# common.yaml
test::groups:
  - name: 'admins'
    type: 1
    rights: []
  - name: 'users'
    type: 2
    rights: []

In erb it's embedded ruby you can use regular ruby functions, on epp it's embedded Puppet so you need to use Puppet functions. There is a to_json_pretty available in stdlib https://forge.puppet.com/modules/puppetlabs/stdlib which will do the job and you can use it like this;

groups = <%= $groups.to_json_pretty %> 

I've just tested it out with;

# test/manifests/jsontest.pp
class test::jsontest {

  $groups = lookup('test::groups')

  file { '/root/epp':
    ensure  => file,
    content => epp("test/jsondata.epp", {groups => $groups})
  }
}
# test/templates/jsondata.epp
groups = <%= $groups.to_json_pretty %> 
# common.yaml
test::groups:
  - name: 'admins'
    type: 1
    rights: []
  - name: 'users'
    type: 2
    rights: []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文