问题
I need to pass a variable in a json_query filter.
This example, with a fixed string, is working correctly (string=tutu) :
- set_fact:
my_value_exist: "{{ my_json.json | json_query('contains(component.name,`tutu`)')}}"
But i need to pass a variable , instead of tutu
- set_fact:
my_value_exist: "{{ my_json.json | json_query('contains(component.name,`{{my_var}}`)')}}"
{{my_var}} is a string retreived in a previous step
Do you have the correct syntax, so that the variable {{my_var}} could be passed correctly in parameter ?
Thanks for your help.
Regards,
回答1:
Use helper variable for a task:
- set_fact:
my_value_exist: "{{ my_json.json | json_query(qry) }}"
vars:
qry: 'contains(component.name,`{{my_var}}`'
来源:https://stackoverflow.com/questions/46038985/ansible-pass-a-variable-in-a-json-query-filter