json_query filter error - template error while templating string:需要标记','

insrf1ej  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(169)

我尝试使用json_query过滤器来解析所需的JSON数据。
Ansible Playbook任务:

- 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-但我得到了上面的错误。

jgwigjjp

jgwigjjp1#

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查询,您在这两个查询中都有部分解决方案,因此,如果您将这两个查询混合在一起,您几乎可以得到您所期望的结果。
稍微解释一下,map的属性是用点.表示法查询的。而数组的元素是用方括号表示法[]查询的。
所以从你第一次尝试开始,这部分是正确的:

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]

请注意:我在这里将单引号'换成了反引号```,正如Ansible文档中关于json_query的一个注解所建议的那样:

在上面的例子中,使用反引号引用文本避免了转义引号并保持了可读性。
实际上,在JMESPath中,它们都表示文字,两者之间有一点小的区别,单引号表示文字表达式,backpacks表示原始文字字符串,但在本例中,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'

相关问题