使用打包器覆盖厨师客户端中的角色属性

Overriding role attributes in chef-client with packer

我有一个厨师角色:

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

我在配置块中使用 Packer 调用:

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

我需要能够从 Packer 中向 recipe_two 添加一些属性。我读到我可以使用 json block of the chef-client 配置器将一些属性添加到 运行 列表。我试过了

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

当我 运行 加壳器时,我可以在 /tmp/packer-chef-client/first-boot.json

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

但是 recipe_two 的 override_attributes 没有暴露在食谱中。我找不到任何如何让它以这种方式工作的示例,也找不到要传递的 "json": {} 块的正确格式。

如果能通过从 Packer 调用的角色向我的食谱公开覆盖的属性,我们将不胜感激

您的问题与加壳器本身无关,而是如何执行 chef-client 并向 chef-client 运行.

提供自定义属性

您可以通过在 chef-client

中包含 --json-attributes(我强烈建议您访问文档,因为它包含示例)来提供自定义属性

-j PATH, --json-attributes PATH

The path to a file that contains JSON data. Used to setup the first client run. For all the future runs with option -i the attributes are expected to be persisted in the chef-server.

所以回到你关于 packer 的问题... 使用您想要覆盖的属性创建一个 json 文件,并确保您使用 --json-attributes 从加壳器调用 chef-client 并将其指向您创建的 json 文件。

您的加壳文件不应包含 override_attributes 部分。相反,它应该是这样的:

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