使用Ansibles uri模块解析非平凡JSON中的值
我从Spark服务器检索到了这个(在所示的示例中,我通过删除许多行来减少它)非平凡的JSON:使用Ansibles uri模块解析非平凡JSON中的值,ansible,uri,Ansible,Uri,我从Spark服务器检索到了这个(在所示的示例中,我通过删除许多行来减少它)非平凡的JSON: { "spark.worker.cleanup.enabled": true, "spark.worker.ui.retainedDrivers": 50, "spark.worker.cleanup.appDataTtl": 7200, "fusion.spark.worker.webui.port": 8082, "fusion.spark.worker.memory": "4
{
"spark.worker.cleanup.enabled": true,
"spark.worker.ui.retainedDrivers": 50,
"spark.worker.cleanup.appDataTtl": 7200,
"fusion.spark.worker.webui.port": 8082,
"fusion.spark.worker.memory": "4g",
"fusion.spark.worker.port": 8769,
"spark.worker.timeout": 30
}
我试图阅读fusion.spark.worker.memory
,但没有成功。在调试语句中,我可以看到以下信息:
msg:Spark memory:{{{Spark\u worker\u cfg.json}}
显示了以下内容:
ok: [process1] => {
"msg": "Spark memory: {u'spark.worker.ui.retainedDrivers': 50, u'spark.worker.cleanup.enabled': True, u'fusion.spark.worker.port': 8769, u'spark.worker.cleanup.appDataTtl': 7200, u'spark.worker.timeout': 30, u'fusion.spark.worker.memory': u'4g', u'fusion.spark.worker.webui.port': 8082}"
}
ok: [process1] => {
"spark_worker_cfg": {
"changed": false,
"connection": "close",
"content_length": "279",
"content_type": "application/json",
"cookies": {},
"cookies_string": "",
"failed": false,
"fusion_request_id": "Pj2zeWThLw",
"json": {
"fusion.spark.worker.memory": "4g",
"fusion.spark.worker.port": 8769,
"fusion.spark.worker.webui.port": 8082,
"spark.worker.cleanup.appDataTtl": 7200,
"spark.worker.cleanup.enabled": true,
"spark.worker.timeout": 30,
"spark.worker.ui.retainedDrivers": 50
},
"msg": "OK (279 bytes)",
"redirected": false,
"server": "Jetty(9.4.12.v20180830)",
"status": 200,
"url": "http://localhost:8765/api/v1/configurations?prefix=spark.worker"
}
}
使用var:spark\u worker\u cfg
的转储显示:
ok: [process1] => {
"msg": "Spark memory: {u'spark.worker.ui.retainedDrivers': 50, u'spark.worker.cleanup.enabled': True, u'fusion.spark.worker.port': 8769, u'spark.worker.cleanup.appDataTtl': 7200, u'spark.worker.timeout': 30, u'fusion.spark.worker.memory': u'4g', u'fusion.spark.worker.webui.port': 8082}"
}
ok: [process1] => {
"spark_worker_cfg": {
"changed": false,
"connection": "close",
"content_length": "279",
"content_type": "application/json",
"cookies": {},
"cookies_string": "",
"failed": false,
"fusion_request_id": "Pj2zeWThLw",
"json": {
"fusion.spark.worker.memory": "4g",
"fusion.spark.worker.port": 8769,
"fusion.spark.worker.webui.port": 8082,
"spark.worker.cleanup.appDataTtl": 7200,
"spark.worker.cleanup.enabled": true,
"spark.worker.timeout": 30,
"spark.worker.ui.retainedDrivers": 50
},
"msg": "OK (279 bytes)",
"redirected": false,
"server": "Jetty(9.4.12.v20180830)",
"status": 200,
"url": "http://localhost:8765/api/v1/configurations?prefix=spark.worker"
}
}
我无法使用{{spark\u worker\u cfg.json.fusion.spark.worker.memory}}
访问该值,我的问题似乎是由包含点的名称引起的:
该任务包含一个带有未定义变量的选项。错误为:
“dict对象”没有属性“fusion”
我看了两篇SO帖子(和)这看起来像是我问题的重复,但无法从中派生出如何解决我当前的问题。数据结构的“json”元素中的键包含文字点,而不是表示结构。这将导致问题,因为如果使用点符号,Ansible将不知道如何将其视为文字。因此,请使用square用括号符号表示,而不是用虚线表示:
- debug:
msg: "{{ spark_worker_cfg['json']['fusion.spark.worker.memory'] }}"
(乍一看,这看起来像是一个需要解码的JSON编码字符串的问题:
“{{spark_worker_cfg.JSON | from_JSON}}”
)您可以使用JSON_查询过滤器获得结果
编辑:
作为对您评论的回应,我们返回了一个空字符串这一事实让我相信查询是不正确的。在使用json_查询过滤器时找到准确的查询可能会让人沮丧,因此我通常会事先使用jsonpath工具。我在下面的评论中链接了一个,但我个人使用intelliJ中的jsonUtils插件来查找我的路径(仍然需要调整,因为这两个路径的处理方式略有不同)
如果您的json如下所示:
{
value: "theValue"
}
然后
会有用的
您传递给json_查询的路径与您尝试执行的操作不正确
如果您的顶级对象名为fusion\u spark\u worker\u memory(不带句点),那么您的查询应该可以运行。我相信,这些点正在抛出一些东西。可能有一种方法可以避开查询中的那些点
编辑2:clockworknet赢了!他两次都赢了我。:bow:不幸的是,这返回了
“msg”:“
我想这是因为查询不太正确,因为额外的点。因为字段的命名方式(fusion.spark.worker.memory),json_查询过滤器认为它正在查找fusion对象中spark对象中spark对象中的worker对象中的内存对象。json查询非常挑剔,您必须完全正确,否则它会发出尖叫声。我建议使用jquery pathfinder在使用json_查询之前帮助您找到所需的确切路径。首先t suggestion returns在({{spark_worker_cfg.json | from_json}}})上发生意外的模板类型错误:预期的字符串或缓冲区“}
是否要更新您的答案以便我可以接受?再次查看,我认为问题在于“json”元素中的键的命名有误导性。{{spark\u worker\u cfg['json']['fusion.spark.worker.memory']}
工作正常吗?是的,这可以工作并返回“msg”:“4g”
OK-希望这能解决您的问题。不清楚“json”元素中的键中的点是否为文字。用点引用数据结构的子元素看起来更干净,但在这种情况下会导致问题。使用方括号表示法稍微难看一点,但可以解决这些歧义。