在官方文档中,variable
只有一个用例用于模板:调用者必须传入一个哈希值。
但对我来说,我有一个非常简单的用例。我只想在sensu客户端配置模板文件client.json.erb
这是模板文件:
{
"client": {
"name": "<%= @server_name %>",
"address": "<%= node.ipaddress %>",
"keepalive": {
"thresholds": {
"warning": 90,
"critical": 180
}
},
"subscriptions": [ "default" ]
}
}
这是我的厨师代码:
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" ]
}
}
如何将变量名称正确传递到模板中?
答案 0 :(得分:7)
这解决了我的问题:
template "/etc/sensu/conf.d/client.json" do
variables(
'server_name': server_name
)
source "sensu-template/conf.d/client.json.erb"
end