【问题标题】:chef template overriding variables through args厨师模板通过参数覆盖变量
【发布时间】:2013-04-17 13:42:06
【问题描述】:

我是 chef-solo opscode 的新手,没有找到以下方法

我正在编写一个模板并将 data_bag json 作为变量传递给它

- 配方

HOME = ""

app_config = data_bag_item("config","app")

template "#{HOME}/app/config/database.yml" do
  local true
  source "#{HOME}/app/config/database.yml.erb"
  variables app_config["database.yml"]
endt

-erb

development:
  adapter: <%= @development["adapter"] %>
  database: <%= @development["database"] %>
  username: <%= @development["username"] %>
  password: <%= @development["password"] %>
  encoding: <%= @development["encoding"] %>
  host: <%= @development["host"] %>

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: <%= @test["adapter"] %>
  database: <%= @test["database"] %>
  username: <%= @test["username"] %>
  password: <%= @test["password"] %>
  encoding: <%= @test["encoding"] %>
  host: <%= @test["host"] %>

production:
  adapter: <%= @production["adapter"] %>
  database: <%= @production["database"] %>
  username: <%= @production["username"] %>
  password: <%= @production["password"] %>
  encoding: <%= @production["encoding"] %>
  host: <%= @production["host"] %>

- 数据包

{
  "id" : "app",

   "database.yml" : {
    "development": {
      "adapter"   : "mysql2",
      "database"  : "app_site",
      "username"  : "root",
      "password"  : "",
      "encoding"  : "utf8",
      "host"      : "localhost"
    } ,
    "production" : {
      "adapter"   : "mysql2",
      "database"  : "app_site",
      "username"  : "root",
      "password"  : "",
      "encoding"  : "utf8",
      "host"      : "localhost"
    } ,
    "test": {
      "adapter"   : "mysql2",
      "database"  : "app_site_test",
      "username"  : "root",
      "password"  : "",
      "encoding"  : "utf8",
      "host"      : "localhost"
    }
  },

  "config.yml":{
     "development":{},
     "production" :{},
     "test":{}
  }
}

一切正常,我想要进一步的是,当我按如下方式执行时

sudo chef-solo -c solo.rb -j solo.json @development["password"]=my_new_passwd

@development["password"] 应该被覆盖为我传递的新值,而不是来自数据包的值

任何提示?

或者对合并两个数据包有什么想法?

已编辑:

我想将 http://apidock.com/rails/Hash/deep_merge 添加到 Chef init 的某个位置,因此 Hash 类将具有可用的 deep_merge 方法,知道该放在哪里吗?我尝试在 recipe 和 solo.rb 的顶部,但没有运气。

【问题讨论】:

  • sudo chef-solo -c solo.rb -j solo.json @development["password"]=my_new_passwd 将不起作用,因为 @development 是命令行上未知的 Ruby 变量名称
  • 我知道,我想做一些类似的工作
  • 问题是你的solo.json 已经是最重要的,最“临时”的方式来传递个人数据给厨师,所以试图覆盖它会很困难。也许您可以尝试将数据放在节点属性而不是 data_bags 中,因为前者更容易覆盖:您将 app_config 放置在分配给节点的角色中,然后通过 solo.json 中给出的节点属性覆盖一些设置。
  • 那太好了@cmur2,任何例子都会有很大帮助。

标签: automation chef-infra chef-recipe chef-solo


【解决方案1】:

https://github.com/sensu/sensu-chef/pull/14#issuecomment-16521845

HOME = "#{ENV['HOME']}"

app_config = data_bag_item("config","app")
override_config = data_bag_item("config","override")

# merging override into app
Chef::Mixin::DeepMerge.deep_merge! override_config["database.yml"], app_config["database.yml"]

template "#{HOME}/app/config/database.yml" do
  local true
  source "#{HOME}/app/config/database.yml.erb"
  variables app_config["database.yml"]
end

【讨论】:

    猜你喜欢
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多