【问题标题】:how to pass 2 data bag variables to template chef如何将 2 个数据包变量传递给模板厨师
【发布时间】:2015-06-24 01:28:47
【问题描述】:

我正在尝试将 2 个数据包作为变量传递到模板中,但它以错误消息结尾。有谁知道我如何将 2 个数据包传递给一个模板?

食谱

db = data_bag_item('dbconnect', 'connection')
dbkey = data_bag_item('database', 'databasename')
template '/etc/config.cnf' do 
    source 'config.cnf.erb'
    action :create
    variables ( 
        :dbcon => db,
        :dbk => dbkey
    )
    end

模板

connection = mysql://<%= @dbcon['dbuser'] %>:<%= @dbcon['dbpasswd'] %>@<%= @dbcon['dbname'] %>/<%= @dbk['dbname'] %>

【问题讨论】:

  • 您可以编辑您的问题以显示错误消息吗?能否也包括数据包的结构?

标签: templates chef-infra databags


【解决方案1】:

好的。我得到了答案。 我错过了变量中的 {} 括号。

db = data_bag_item('dbconnect', 'connection')
dbkey = data_bag_item('database', 'databasename')
template '/etc/config.cnf' do 
  source 'config.cnf.erb'
  action :create
  variables ({ 
    :dbcon => db,
    :dbk => dbkey
  })
end

【讨论】:

  • @Tensibai 请参阅acrmp.github.io/foodcritic/#FC034(但在示例中缺少逗号)
  • 还有另一个例子:github.com/opscode-cookbooks/chef-client/blob/master/recipes/… - 现在使用 Ruby 1.9+,您还可以使用“key: value”语法而不是“hash-rocket”(“=>”)
  • @Tensibai 你能在你的 cmets 中少一些攻击性吗?谢谢
  • 感谢 Ronald 当我们将值作为字符串传递时, (....) 工作正常,但是当我尝试传递数据包(或嵌套哈希)时,我需要 ({..... })
猜你喜欢
  • 2015-03-08
  • 1970-01-01
  • 2016-12-11
  • 2016-08-02
  • 2021-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多