【问题标题】:How to use custom JSON attributes in Chef recipe如何在 Chef 食谱中使用自定义 JSON 属性
【发布时间】:2016-03-29 17:16:40
【问题描述】:

我是 JSON 新手。我在 AWS Opswork 中创建了自定义 JSON,并尝试将其作为 Chef 配方中的属性进行访问,但不幸的是它没有捕获 JSON 值。我的 JSON 文件看起来像..

{

 "normal": { 
 "filebeat_minjar": {
 "log_path" : "/var/log/*.log",
 "hosts" : "Some Random Host ID", 
 "port"  : 5000
 }
 }
}

我正试图在食谱中捕捉它,

log = node['filebeat_minjar']['log_path']
hosts = node['filebeat_minjar']['hosts']
port = node['filebeat_minjar']['port']

但它失败了,我也试过没有'正常'。我得到了一些 []null 类错误。

【问题讨论】:

  • 你可以用 filebeat 代替 filbeat 吗?

标签: json chef-infra aws-opsworks


【解决方案1】:

试试这个方法,

log = node['normal']['filbeat_minjar']['log_path']
hosts = node['normal']['filbeat_minjar']['hosts']
port = node['normal']['filbeat_minjar']['port']

log = node.normal.filbeat_minjar.log_path
hosts = node.normal.filbeat_minjar.hosts
port = node.normal.filbeat_minjar.port

Json 对象就像一棵树,元素就是树枝。 希望对您有所帮助

【讨论】:

  • 不要这样做,节点数据格式不正确,应该修复。切勿将第二种形式用于任何属性访问,它已被弃用,并将在未来的版本中删除。
【解决方案2】:

您的 Chef 代码是正确的,但您需要修复 JSON。您不需要那里的"normal": {...},Chef 和 OpsWorks 会为您处理。

【讨论】:

    【解决方案3】:

    以下对我有用。

    自定义 JSON

    {
        "Production": { 
            "ApplicationLayer": {
                "DockerTag" : "Version1"
            }
        }   
    }
    

    从厨师食谱中调用。

    node[:Production][:ApplicationLayer][:DockerTag]
    

    【讨论】:

      猜你喜欢
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      相关资源
      最近更新 更多