5

I have a chef system where we have multiple environments and have attributes set in evironment JSON.

I'm having trouble accessing these from within cookbook attribute files and recipes.

Kenny Rasschaert
  • 8,925
  • 3
  • 41
  • 58
paul bruno
  • 51
  • 1
  • 1
  • 2

3 Answers3

2

What does your environment file look like? Are you setting default or override attributes? If you're setting default, note that is a fairly low priority level in the attributes chain, so it might be overridden by a recipe, or a role.

The precedence of the attributes is as follows, from low to high:

  1. default attributes applied in an attributes file
  2. default attributes applied in an environment
  3. default attributes applied in a role
  4. default attributes applied on a node directly in a recipe
  5. normal or set attributes applied in an attributes file
  6. normal or set attributes applied on a node directly in a recipe
  7. override attributes applied in an attributes file
  8. override attributes applied in a role
  9. override attributes applied in an environment
  10. override attributes applied on a node directly in a recipe

Above from:

jtimberman
  • 7,511
  • 2
  • 33
  • 42
1

Finally I was able to use environment's attribute in the Chef recipe. Lets say we have an environment like this:

{
  "name": "QA",
  "description": "QA environment",
  "cookbook_versions": {
  },
  "json_class": "Chef::Environment",
  "chef_type": "environment",
  "default_attributes": {
    "comp_rsyslog": {
      "filetag_env": "compqa"
    }
  },
  "override_attributes": {
  }
}

And we need to use the filetag_env attribute in the service's template config file to pass the environment attribute.

The way I do in the conf erb file it's like:

$InputFileTag <%= node['comp_rsyslog']['filetag_env'] %>,<%= node['rsyslog']['filetag1'] %>

The <%= node['rsyslog']['filetag1'] %> is defined in the recipe attribute's file:

default['rsyslog']['filetag1'] = 'comp_service'

The result will be a file in /etc/rsyslog.d/comp_service.conf with a content like:

$InputFileTag compqa,comp_service.
Gabriel
  • 61
  • 1
  • 6
0

The correct order is now, as taken from the chef documentation site:

  1. A default attribute located in a cookbook attribute file
  2. A default attribute located in a recipe
  3. A default attribute located in an environment
  4. A default attribute located in role
  5. A force_default attribute located in a cookbook attribute file
  6. A force_default attribute located in a recipe
  7. A normal attribute located in a cookbook attribute file
  8. A normal attribute located in a recipe
  9. An override attribute located in a cookbook attribute file
  10. An override attribute located in a recipe
  11. An override attribute located in a role
  12. An override attribute located in an environment
  13. A force_override attribute located in a cookbook attribute file
  14. A force_override attribute located in a recipe
  15. An automatic attribute identified by Ohai at the start of the chef-client run
Reaces
  • 5,547
  • 4
  • 36
  • 46
  • Note that you can also blacklist or whitelist attributes, which serves to make Chef one of the most convoluted configuration management tools I've ever used. https://docs.chef.io/attributes.html#attribute-precedence – dragon788 Aug 27 '18 at 15:41