如何在厨师模板中设置变量?

安东尼·孔

在官方文档中,variablefor模板只有一种用例:调用者必须传递哈希。

但是对我来说,我有一个非常简单的用例。我只想在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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章