Using attributes in Chef Solo JSON

半世苍凉 提交于 2019-12-06 10:04:43

问题


Is it possible to specify attribute values in Chef Solo's JSON? I have a solo.json with a run list and I would like to specify the attributes there. The Chef documentation seems to indicate it should be possible for me to do something like:

{
    "hostname": {
        "test": "value2"
    },
    "default_attributes": {
        "hostname": {
            "test": "value3"
        }
    },
    "override_attributes": {
        "hostname": {
            "test": "value4"
        }
    },
    "default": {
        "hostname": {
            "test": "value5"
        }
    },
    "run_list": [
        "recipe[hostname::default]"
    ]
}

However, whenever I try to access the value in my recipe:

p node['hostname']['test']

I just get the value defined in attributes/default.rb, and if I do not define it there, I get a nil value back.

Is there a way to reference these values?


回答1:


The only level of attributes you can store in the node data is normal, everything else is reset at the start of the converge and rebuilt from roles, environments, and cookbooks. You want something like looks like this:

{
  "normal": {
    "hostname": {
      "test": "something"
    }
  },
  "run_list": [
    "recipe[hostname::default]"
  ]
}



回答2:


I tried using the above example for setting attributes in a JSON file for chef-solo, and the "normal": {...} block was completely ignored. The run list was read, but attributes never seem to work.

My command is:

chef-solo -c /path/to/config_file.rb -j /path/to/file.json

My JSON file:

{
  "name": "my_json_file",
  "description": "JSON run-list and attributes.",
  "normal": {
    "my_cookbook": {
      "git_branch": "staging"
    }
  },
  "run_list": [
    "recipe[my_cookbook::recipe1]",
    "recipe[my_cookbook::recipe2]",
  ]
}

During the converges, Chef went straight back to the default values inside of attributes/default.rb. Are the JSON values being entered correctly? So far, my only workaround has been to create a new recipe for each "git_branch": attribute I want tested with chef-solo, and adding that updated run-list to a different JSON file. Essentially, copying an entire recipe to change a single node.normal['my_cookbook']['git_branch'] value. Needless to say, this shouldn't be a solution.

Using Chef 14.0.202




回答3:


Follow up:

Not setting the attribute precedence level inside the JSON file with Chef Solo works. The JSON should look similar to this:

{
  "name": "my_json_file",
  "description": "JSON run-list and attributes.",
  "my_cookbook": {
    "git_branch": "staging"
  },
  "run_list": [
    "recipe[my_cookbook::recipe1]",
    "recipe[my_cookbook::recipe2]",
  ]
}

Not including the "normal": {...} or "default_attributes": {...}, etc, will pass attributes for chef-solo to use.



来源:https://stackoverflow.com/questions/24010272/using-attributes-in-chef-solo-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!