gpt4 book ai didi

ruby - 如何在 Chef 模板中设置变量?

转载 作者:太空宇宙 更新时间:2023-11-03 17:10:09 25 4
gpt4 key购买 nike

在官方文档中,模板只有一种variable的用例:调用者必须传入一个hash。

但是对我来说,我有一个非常简单的用例。我只想在 sensu 客户端配置模板文件中设置一个服务器名称 client.json.erb

这是模板文件:

{
"client": {
"name": "<%= @server_name %>",
"address": "<%= node.ipaddress %>",
"keepalive": {
"thresholds": {
"warning": 90,
"critical": 180
}
},
"subscriptions": [ "default" ]
}
}

这是我的 Chef 代码:

server_name = "server1.example.com"

template "/etc/sensu/conf.d/client.json" do
variables({
:server_name => server_name
})
source "sensu-template/conf.d/client.json.erb"
end

配置文件变成了:

{
"client": {
"name": "{}",
"address": "10.0.1.1",
"keepalive": {
"thresholds": {
"warning": 90,
"critical": 180
}
},
"subscriptions": [ "default" ]
}
}

我应该如何将变量名正确地传递到模板中?

最佳答案

这解决了我的问题:

template "/etc/sensu/conf.d/client.json" do
variables(
'server_name': server_name
)
source "sensu-template/conf.d/client.json.erb"
end

关于ruby - 如何在 Chef 模板中设置变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32580587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com