How to override default attributes in chef

I'm new to Chef, although I understand the concept now. I understand the concept of default and overriding attributes, but how do I do it in the environment? Specifically, I want to override the data bag attributes that someone else has defined. For example, if we have the following data packet

{
   "id" : "common",
   "some_server" : "www.some_server.com"
}

      

I want ALL my recipes in my "environment" to override "some_server" from "www.my_server.com". Does this make sense? Thank!

+3


source to share


2 answers


I followed the advice of this [article] [1] [1]: http://www.getchef.com/blog/2014/01/23/attributes-or-data-bags-what-should-i-use/ which recommends using datasets for "global" values, that is, values ​​that will be used by all recipes, regardless of environment, and for encoding variables that are environment specific in my environments, and set my nodes to that environment. Therefore, it is not recommended to place the following in data bags:

{
  "id":"common",
  "devel": {
    "some_server": ""www.some_server.com"
  },
  "staging": {
    "some_server: "www.my_server.com"
  }
}

      

Instead, I do the following

Say in my cookbook setup_server I may have the following attribute defined in the /default.rb attributes

default.setup_server.a_server = "www.some_server.com"

      

... and a recipe that just does

puts "server " + "#{node.setup_server.a_server}"

      



If I assign my node to the _default environment, the output is:

server = www.some_server.com

      

Now I create an environment named my_environment and define the following overridden attribute

environments/my_environment.json:
{
  "name"  : "my_environment",
  "override_attributes" : {
    "setup_server" : {
      "a_server": "www.my_server.com"
    }
  }
}

      

If I now assign my cooking to "my_environment" and re-earn the recipe, I now get

server = www.my_server.com

      

+1


source


A data packet is just a bag of data - a list of key-value pairs. It has nothing to do with environments, attributes, or anything else. If you want your dataset to provide different data based on the environment, you must learn it.

One option is to have the environment name as part of the data:

{
  "id":"common",
  "devel": {
    "some_server": ""www.some_server.com"
  },
  "staging": {
    "some_server: "www.my_server.com"
  }
}

      



And then you can read your data in the recipe:

data_bag[node.environment]["some_server"]

      

if you have a development and middleware environment.

+1


source







All Articles