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
此方法记录在哪里?此方法记录在哪里?