JMESPathTypeError 在 Ansible 中使用 json_query 过滤器和 starts_with 时出错 [英] JMESPathTypeError when using json_query filter in Ansible with starts_with

查看:24
本文介绍了JMESPathTypeError 在 Ansible 中使用 json_query 过滤器和 starts_with 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试过滤来自 Ansible 中 boto3 的结果.

I am trying to filter results that arrived from boto3 in Ansible.

当我在没有[?starts_with(...)]"的结果上使用 json 查询时它运行良好,但是当添加 starts_with 语法时:

When I use json query on the results without the "[?starts_with(...)]" it works well, but when adding the starts_with syntax:

"state_machines[?starts_with(name,'hello')].state_machine_arn"

为了过滤结果:

{u'boto3': u'1.4.4', u'state_machines': 
[{u'state_machine_arn': u'<state machine arn 1>', u'name': u'hello_world_sfn', u'creation_date': u'2017-05-16 14:26:39.088000+00:00'}, 
{u'state_machine_arn': u'<state machine arn 2>', u'name': u'my_private_sfn', u'creation_date': u'2017-06-08 07:25:49.931000+00:00'}, 
{u'state_machine_arn': u'<state machine arn 3>', u'name': u'alex_sfn', u'creation_date': u'2017-06-14 08:35:07.123000+00:00'}], 
u'changed': True}" }

我希望得到第一个 state_machine_arn 值:state machine arn 1"

I expect to get the first state_machine_arn value: "state machine arn 1"

但相反,我得到了例外:

But instead, I get the exception:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: JMESPathTypeError: In function contains(), invalid type for value: <lamdba_name>, expected one of: ['array', 'string'], received: "unknown" fatal: [localhost]: FAILED!
=> {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}

可能是什么问题?

推荐答案

问题是 json_query 过滤器期望得到一个带有 ascii 字符串的字典,但是你提供的是 unicode 字符串(注意 u'blabla' 在您的输入中).

The problem is that json_query filter expects to get a dictionary with ascii strings, but what you're providing it are unicode strings (notice the u'blabla' in your input).

这是 json_query 的一个问题,显然是在 Ansible 2.2.1 中引入的(虽然不是很清楚),这里有更多细节:https://github.com/ansible/ansible/issues/20379#issuecomment-284034650

This is an issue with json_query that apparently got introduced in Ansible 2.2.1 (although that is not really clear), here are some more details: https://github.com/ansible/ansible/issues/20379#issuecomment-284034650

我希望这会在未来的版本中得到修复,但目前这是一种对我们有用的解决方法:

I hope this gets fixed in a future version, but for now this is a workaround that worked for us:

"{{ results | to_json | from_json | json_query(jmespath_query) }}"

其中 jmespath_query 是一个包含 starts_with 查询的变量.这个往返 json 的技巧将 unicode 字符串转换为 ASCII 字符串 :)

Where jmespath_query is a variable that contains a starts_with query. This trick of going to and from json turns the unicode strings into ASCII ones :)

这篇关于JMESPathTypeError 在 Ansible 中使用 json_query 过滤器和 starts_with 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆