2015-12-23 77 views
0

我是JSON的新手。我在AWS Opswork中创建了自定义JSON,并尝试将其作为Chef recipe的属性进行访问,但不幸的是它没有捕获JSON值。我的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'] 

但它失败了,我也尝试没有“正常”。我得到了一些[]空类错误。

+0

你也许是男人_filebeat_而不是_filbeat_? – StephenKing

回答

0

尝试这种方式,

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对象是如树,该元素是分支。 希望这个帮助

+0

谢谢路易斯.. !!有效。 –

+0

不要这样做,节点数据格式不正确,应该修复。切勿将第二种形式用于任何属性访问,它将被弃用,并将在未来的版本中删除。 – coderanger

0

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

0

以下为我工作。

自定义JSON

{ 

“生产”:{ “应用层”:{ “DockerTag”: “版本1” } }

}

从厨师食谱调用。

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