我正在尝试使用json_query过滤器来解析所需的JSON数据。
可供选择的剧本任务:
- name: Print items in option
ansible.builtin.debug:
msg: "{{ clust_opts | community.general.json_query('defaultReplicaSet.topology.\"dc1-indb-tst1:3306\" | [].option['memberWeight']')' }}"我还在我的剧本中尝试了以下几点--这导致了同样的错误:
- name: Print items in option
ansible.builtin.debug:
msg: "{{ clust_opts | community.general.json_query('defaultReplicaSet.topology.[?option == 'memberWeight']')' }}"错误信息:
TASK [Print items in option] **************************************************************************************************
fatal: [dc1-indb-tst1]: FAILED! => {"msg": "template error while templating string: expected token ',', got 'memberWeight'. String: {{ clust_opts | json_query('defaultReplicaSet.topology.[?option == 'memberWeight']')' }}"}JSON来解析以下数据:
{
"clusterName": "idb_test",
"defaultReplicaSet": {
"globalOptions": [
{
"option": "groupName",
"value": "8f9e6040-F10s-11ec-b244-005056a4645c",
"variable": "group_replication_group_name"
},
{
"option": "memberSslMode",
"value": "REQUIRED",
"variable": "group_replication_ssl_mode"
},
{
"option": "disableClone",
"value": false
}
],
"tags": {
"dc1-indb-tst1:3306": [],
"dc1-indb-tst2:3306": [],
"dc1-indb-tst3:3306": [],
"global": []
},
"topology": {
"dc1-indb-tst1:3306": [
{
"option": "autoRejoinTries",
"value": "3",
"variable": "group_replication_autorejoin_tries"
},
{
"option": "consistency",
"value": "EVENTUAL",
"variable": "group_replication_consistency"
},
{
"option": "exitStateAction",
"value": "READ_ONLY",
"variable": "group_replication_exit_state_action"
},
{
"option": "expelTimeout",
"value": "5",
"variable": "group_replication_member_expel_timeout"
},
{
"option": "groupSeeds",
"value": "dc1-indb-tst2:33061,dc1-indb-tst3:33061",
"variable": "group_replication_group_seeds"
},
{
"option": "ipAllowlist",
"value": "AUTOMATIC",
"variable": "group_replication_ip_allowlist"
},
{
"option": "ipWhitelist",
"value": "AUTOMATIC",
"variable": "group_replication_ip_whitelist"
},
{
"option": "localAddress",
"value": "dc1-indb-tst1:33061",
"variable": "group_replication_local_address"
},
{
"option": "memberWeight",
"value": "50",
"variable": "group_replication_member_weight"
},
{
"value": "WRITESET",
"variable": "binlog_transaction_dependency_tracking"
},
{
"value": "LOGICAL_CLOCK",
"variable": "replica_parallel_type"
},
{
"value": "4",
"variable": "replica_parallel_workers"
},
{
"value": "ON",
"variable": "replica_preserve_commit_order"
},
{
"value": "XXHASH64",
"variable": "transaction_write_set_extraction"
}
],
"dc1-indb-tst2:3306": [
{
"option": "autoRejoinTries",
"value": "3",
"variable": "group_replication_autorejoin_tries"
},
{
"option": "consistency",
"value": "EVENTUAL",
"variable": "group_replication_consistency"
},
{
"option": "exitStateAction",
"value": "READ_ONLY",
"variable": "group_replication_exit_state_action"
},
{
"option": "expelTimeout",
"value": "5",
"variable": "group_replication_member_expel_timeout"
},
{
"option": "groupSeeds",
"value": "dc1-indb-tst1:33061,dc1-indb-tst3:33061",
"variable": "group_replication_group_seeds"
},
{
"option": "ipAllowlist",
"value": "AUTOMATIC",
"variable": "group_replication_ip_allowlist"
},
{
"option": "ipWhitelist",
"value": "AUTOMATIC",
"variable": "group_replication_ip_whitelist"
},
{
"option": "localAddress",
"value": "dc1-indb-tst2:33061",
"variable": "group_replication_local_address"
},
{
"option": "memberWeight",
"value": "50",
"variable": "group_replication_member_weight"
},
{
"value": "WRITESET",
"variable": "binlog_transaction_dependency_tracking"
},
{
"value": "LOGICAL_CLOCK",
"variable": "replica_parallel_type"
},
{
"value": "4",
"variable": "replica_parallel_workers"
},
{
"value": "ON",
"variable": "replica_preserve_commit_order"
},
{
"value": "XXHASH64",
"variable": "transaction_write_set_extraction"
}
],
"dc1-indb-tst3:3306": [
{
"option": "autoRejoinTries",
"value": "3",
"variable": "group_replication_autorejoin_tries"
},
{
"option": "consistency",
"value": "EVENTUAL",
"variable": "group_replication_consistency"
},
{
"option": "exitStateAction",
"value": "READ_ONLY",
"variable": "group_replication_exit_state_action"
},
{
"option": "expelTimeout",
"value": "5",
"variable": "group_replication_member_expel_timeout"
},
{
"option": "groupSeeds",
"value": "dc1-indb-tst1:33061,dc1-indb-tst2:33061",
"variable": "group_replication_group_seeds"
},
{
"option": "ipAllowlist",
"value": "AUTOMATIC",
"variable": "group_replication_ip_allowlist"
},
{
"option": "ipWhitelist",
"value": "AUTOMATIC",
"variable": "group_replication_ip_whitelist"
},
{
"option": "localAddress",
"value": "dc1-indb-tst3:33061",
"variable": "group_replication_local_address"
},
{
"option": "memberWeight",
"value": "50",
"variable": "group_replication_member_weight"
},
{
"value": "WRITESET",
"variable": "binlog_transaction_dependency_tracking"
},
{
"value": "LOGICAL_CLOCK",
"variable": "replica_parallel_type"
},
{
"value": "4",
"variable": "replica_parallel_workers"
},
{
"value": "ON",
"variable": "replica_preserve_commit_order"
},
{
"value": "XXHASH64",
"variable": "transaction_write_set_extraction"
}
]
}
}
}最终目标是能够在option字段中查找数据并返回与其关联的value。
因此,在我的示例中,我希望找到特定系统的memberWeight并返回value。
我正在查看dc1-indb-tst1:3306,特别是memberWeight,它应该返回50 -但是我得到了上面的错误。
发布于 2022-06-07 19:53:03
TL;DR;
在这个用例中,一个有效的调试任务是:
- ansible.builtin.debug:
msg: >-
{{
clust_opts | community.general.json_query('
defaultReplicaSet
.topology
."dc1-indb-tst1:3306"[?
option == `memberWeight`
].value | [0]
')
}}这意味着:
ok: [localhost] =>
msg: '50'您所收到的错误是因为您忘记在'查询中转义单引号JMESPath。
用引号转义来缓解你自己的生活的一个想法是使用YAML折叠块表示法
- ansible.builtin.debug:
msg: >-
I don't have to escape single (')
nor double (") quote in this string,
it is delimited only by the fact that it is further indented
than the block where the folded block starts.现在,对于您的JMESPath查询,在这两个查询中都有部分解决方案,因此,如果将两者混合在一起,您几乎可以得到预期的结果。
一个小小的解释词,地图的性质被询问用点.符号。而数组的元素是通过方括号符号[]查询的。
所以,从你的第一次尝试,这部分是正确的:
defaultReplicaSet.topology."dc1-indb-tst1:3306"在第二部分,这一部分是正确的:
[?option == 'memberWeight']你们在一起
defaultReplicaSet
.topology
."dc1-indb-tst1:3306"[?
option == 'memberWeight'
]它给出了整个对象:
[
{
"option": "memberWeight",
"value": "50",
"variable": "group_replication_member_weight"
}
]现在只需查询该值,并返回它的第一个结果,因此添加:
.value | [0]管道表达式是用来停止投影的,有关这方面的更多资料,请参阅有关文件。。
因此,您的查询结果是:
defaultReplicaSet
.topology
."dc1-indb-tst1:3306"[?
option == `memberWeight`
].value | [0]--请注意:我把单引号'换成了后面的`,这是json_query中的提议。
在上面的例子中,使用backticks引用文字可以避免转义引号并保持可读性。
实际上,在JMESPath中,它们都代表文字。在这两者之间有一个细微的区别,单引号是一个文字表达式。和背包表示原始的文字字符串。,但在手头的情况下,is将采取相同的行动。
您的debug任务应该是:
- ansible.builtin.debug:
msg: >-
{{
clust_opts | community.general.json_query('
defaultReplicaSet
.topology
."dc1-indb-tst1:3306"[?
option == `memberWeight`
].value | [0]
')
}}这将产生:
TASK [ansible.builtin.debug] *********************************************
ok: [localhost] =>
msg: '50'https://stackoverflow.com/questions/72535973
复制相似问题