Using roles to override attributes with numerous [sections]


I’m hoping this very simple as i can find lots of examples… I just can’t get it to work with the number of attribute names [] i’m using.


if in master cookbook I have the following in an attribute file :

default[‘MyApplication’][‘SubComponent’][‘PrimaryInstance’][‘ID’] ='1’
default[‘MyApplication’][‘SubComponent’][‘SecondaryInstance’][‘ID’] =‘2’

In my wrapper cookbook I have a role called “NodeB”. I want to use this to override the ID with 3 & 4 like this :

default[‘MyApplication’][‘SubComponent’][‘PrimaryInstance’][‘ID’] ='3’
default[‘MyApplication’][‘SubComponent’][‘SecondaryInstance’][‘ID’] =‘4’

I’ve tried the following as a json file to create the role :

“name”: “MyApplication”,
“json_class”: “Chef::Role”,
“description”: “Ensures ID is unique”,
“run_list”: [“recipe[MyApplication::server_instance1]”,“recipe[MyApplication::instance2]”],
“override_attributes”: (“MyApplication” => {“SubComponent” => {“PrimaryInstance” => {“ID” => “3”}}})
“override_attributes”: (“MyApplication” => {“SubComponent” => {“SecondaryInstance” => {“ID” => “4”}}})

but this fails.

There are lots of examples on the internet when the attributes have fewer sections but cant seem to get this to work where I have four.

Many thanks for anyone who’s able to take a look at this and suggest a fix.


You’re mixing Ruby hash syntax with JSON hash syntax. If you are in a JSON file that needs to be:

  "name": "MyApplication",
  "json_class": "Chef::Role",
  "description": "Ensures ID is unique",
  "run_list": ["recipe[MyApplication::server_instance1]", "recipe[MyApplication::instance2]"],
  "override_attributes": {
    "MyApplication": {
      "SubComponent": {
        "PrimaryInstance": {
          "ID": "3"
        "SecondaryInstance": {
          "ID": "4"
1 Like

Many thanks coderanger, the Chef server has accepted that code through its parsing and uploaded to the server. Appreciate your help with this.

For whatever reason the override is not actually being called, even though assigned as a role to the node, but will investigate that next.