在Chef的recipe中如何访问normal属性

51次阅读
没有评论

问题描述

在使用Chef时,他在节点的normal部分中添加了一个属性,并希望在recipe中使用这个属性的值。他尝试使用node['myattribute']来访问这个属性,但返回的是空值。他想知道如何在recipe中正确地访问normal属性。

解决方案

请注意以下操作注意版本差异及修改前做好备份。
根据Chef的文档,你应该使用normal属性来访问节点数据。
“normal”属性是持久存在于节点对象中的设置。normal属性的属性优先级高于默认属性。
你可以在这里阅读更多相关信息:
https://docs.chef.io/attributes.html
https://docs.chef.io/knife_node.html
以下是一些示例:

"normal": {
  "ebs": {
    "devices": [
      {
        "mount_device": "/dev/xvdf",
        "encrypt": true,
        "volume_mapping": [
          {
            "device": "/dev/xvdf"
          }
        ]
      }
    ]
  }
}

在你的recipe中可以这样使用:

if node.ebs.devices[0]['mount_device'] == '/dev/xvdf' and node.ebs.devices[0]['encrypt']
  # do something
end

另一个示例中使用了node['myattribute']

"normal": {
  "myattribute": "myvalue"
}

你可以在recipe中这样使用:

myvalue = node['myattribute']
# do something with myvalue

请注意,如果你在recipe中仍然无法访问normal属性,请确保你的属性设置正确,并且没有其他因素导致无法访问。

正文完