问题
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
回答1:
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"
}
}
回答2:
your issue has nothing to do with packer itself, but rather how to execute chef-client
and provide custom attribute to the chef-client run.
you can provide custom attributes by including --json-attributes (i strongly advise you to visit the documentation, since it holds examples) in chef-client
-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.
so back to your question with packer...
create a json file with the attributes which you would like to override and make sure that you invoke chef-client from packer with the --json-attributes
and point it to the json file you created.
来源:https://stackoverflow.com/questions/54654756/overriding-role-attributes-in-chef-client-with-packer