Given the following playbook:
---
- name: "foo bar"
hosts: localhost
connection: local
gather_facts: false
vars:
foo:
-
a: aa
b: bb
-
a: cc
b: dd
tasks:
- debug:
msg: " filter {{foo}} to {{ foo | json_query(query)}} "
vars:
bar: ['dd','ee']
query: "[?a == 'cc' && contains (['dd','ee'],b)]"
#query: "[?a == 'cc' && contains ( {{bar}} ,b)]"
I would like to pass a variable defined in ansible bar: ['dd','ee']
to a jmes_path query like "[?a == 'cc' && contains ( {{bar}} ,b)]"
. Unfortunately, this does not work, the script fails.
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value ([]), and could not be converted to an dict. Error was: Expecting: comma, got: literal: Parse error at column 28, token \"dd\" (LITERAL), for expression:\n\"[?a == 'cc' && contains ( [u'dd', u'ee'] ,b)]\"\n ^\n\nThe error appears to have been in '/home/vagrant/testnew/lieferschein-deployment/stack.yml': line 16, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - debug:\n ^ here\n"}
However, defining the list in the query itself, inline like "[?a == 'cc' && contains (['dd','ee'],b)]"
, it works without problems
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": " filter [{u'a': u'aa', u'b': u'bb'}, {u'a': u'cc', u'b': u'dd'}] to [{u'a': u'cc', u'b': u'dd'}] "
}
Is it possible to put an Ansible variable into the query, and if so, how?