I am trying to filter results that arrived from boto3 in Ansible.
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"
In order to filter results:
{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}" }
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": ""}
What can be the problem?
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).
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) }}"
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With