【问题标题】:Chef node attributesChef节点属性
【发布时间】:2017-10-24 00:42:01
【问题描述】:

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

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 可能不存在,具体取决于环境,我想在获取属性桶之前检查一下。我该怎么做?

【问题讨论】:

  • 只是ruby ;-)
  • 你能举个例子吗?我正在使用厨师,但我不使用红宝石。这是厨师的错误:chef undefined method '[]' for nil:NilClass

标签: ruby chef-infra


【解决方案1】:

对于较新的 Chef,您可以使用 node.read 方法:

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

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

【讨论】:

  • 这个方法记录在哪里?
【解决方案2】:
s3_bucket = node['elastic']['s3']['bucket'] if node['elastic']['s3']

【讨论】:

    猜你喜欢
    • 2016-02-29
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多