Search code examples
chef-infrachef-recipepacker

Overriding role attributes in chef-client with packer


I have a chef role:

{
  "name": "my-role",
  "description": "Defines a role",
  "override_attributes": {
    "cookbook_one" {
      "key": "value"
    }
  }
  "run_list": [
    recipe["cookbook_one"],
    recipe["cookbook_two"]
  ]
}

Which I call with Packer in the provisioner block:

{
  "variables": {
    "my-variable": ""
  },
  "provisioners": [
    {
      "type": "chef-client",
      "server_url": "https://mychefserver.com/",
      "run_list": "role[my-role]",
      ...
    }

I need to be able to add some attributes to recipe_two from within Packer. I read I can use the json block of the chef-client provisioner to add some attributes to the runlist. I tried

  "type": "chef-client",
  "server_url": "https://mychefserver.com/",
  "run_list": "role[my-role]",
  "json": {
    "override_attributes": {
      "cookbook_two": {
        "some_key": "value"
      }
    }
  }

and when I run packer I can see in /tmp/packer-chef-client/first-boot.json

   {
     "override_attributes": {
       "cookbook_two": {
         "some_key": "{{ user `my-variable` }}"
       }
     },
     "run_list": [
       "role[my-role]"
     ]
   }

But the override_attributes for recipe_two are not exposed to the cookbook. I cannot find any examples of how to get it to work in this way nor the correct format of the "json": {} block to pass through.

Any direction to exposing overridden attributes to my cookbook through the role called from Packer would be greatly appreciated


Solution

  • Your packer file should not include the override_attributes part. Instead it should be like this:

    "type": "chef-client",
      "server_url": "https://mychefserver.com/",
      "run_list": "role[my-role]",
      "json": {
        "cookbook_two": {
          "some_key": "value"
        }
      }