如何在厨师模板中设置变量?
How to set a variable in a chef template?
在官方文档中,template只有一个用例variable
:调用者必须传入一个hash。
但是对我来说,我有一个非常简单的用例。我只想在 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" ]
}
}
我应该如何将变量名正确地传递到模板中?
这解决了我的问题:
template "/etc/sensu/conf.d/client.json" do
variables(
'server_name': server_name
)
source "sensu-template/conf.d/client.json.erb"
end
在官方文档中,template只有一个用例variable
:调用者必须传入一个hash。
但是对我来说,我有一个非常简单的用例。我只想在 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" ]
}
}
我应该如何将变量名正确地传递到模板中?
这解决了我的问题:
template "/etc/sensu/conf.d/client.json" do
variables(
'server_name': server_name
)
source "sensu-template/conf.d/client.json.erb"
end