Ruby 厨师节点属性

Ruby 厨师节点属性,ruby,chef-infra,Ruby,Chef Infra,我想检查属性的以下路径是否存在: s3_bucket = node['elastic']['s3']['bucket'] 这是json: "elastic": { "version": "5.4.1", "cluster": "cluster-dev", "node": "node1", "host": "localhost", "port": 9201, "username": "elasticsearch", "groupname": "elast

我想检查属性的以下路径是否存在:

s3_bucket = node['elastic']['s3']['bucket']
这是json:

"elastic": {
   "version": "5.4.1",
   "cluster": "cluster-dev",
   "node": "node1",
   "host": "localhost",
   "port": 9201,
   "username": "elasticsearch",
   "groupname": "elasticsearch",
   "s3" : {
      "bucket": "hello"
   }
}

根据环境的不同,字段s3可能不存在,我想在获取属性bucket之前检查一下。我如何做到这一点?

对于较新的厨师,您可以使用
节点。阅读
方法:

s3_bucket = node['elastic']['s3']['bucket'] if node['elastic']['s3']
s3_bucket = node.read('elastic', 's3', 'bucket')

如果任何中间键不存在,则为
nil

对于较新的Chef,您可以使用
节点。读取
方法:

s3_bucket = node.read('elastic', 's3', 'bucket')

如果任何中间键不存在,它将是
nil

它只是;-)你能举个例子吗?我用的是厨师,但我不用ruby。这是来自chef的错误:
chef未定义的方法'[],用于nil:NilClass
它只是;-)你能举个例子吗?我用的是厨师,但我不用ruby。这是来自chef:
chef未定义的方法“[]”对于nil:NilClass
此方法记录在哪里?此方法记录在哪里?