1
votes

I am new to chef-solo opscode and did not find out the way to the following

I am writing one template and passing data_bag json as variables to it

- recipe

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"] %>

- data bag

{
  "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":{}
  }
}

Its all working well, and what I want further is, while I execute it as below

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

@development["password"] should get overriden to my new value passed rather than from data bag

ANy hints?

Or any idea about merging two data bags?

Edited:

I want to add http://apidock.com/rails/Hash/deep_merge this to somewhere at chef init, so Hash class will have deep_merge method available, any idea where to put this? I tried at the top of recipe and solo.rb but no luck.

1
sudo chef-solo -c solo.rb -j solo.json @development["password"]=my_new_passwd will not work because @development is a Ruby variable name not known on the command line - cmur2
I know, I want to make something similar work - Amol Pujari
The thing is your solo.json is already the most important, most "adhoc" way to pass individual data to Chef so trying to override that will be hard. Maybe you can try to place your data in node attributes instead of data_bags because the former allow easier overwriting: you place your app_config in a role that you assign to your node and then overwrite some settings via the node properties given in solo.json. - cmur2
thats great @cmur2, any example would be great help. - Amol Pujari

1 Answers

1
votes

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