鉴于以下剧本:
---
- 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)]"
我想将ansible bar: ['dd','ee']
中定义的变量传递给像"[?a == 'cc' && contains ( {{bar}} ,b)]"
这样的jmes_path查询。不幸的是,这不起作用,脚本失败了。
致命:[localhost]:失败! => {“失败”:是的,“msg”:“该字段 'args'的值无效([]),无法转换为 字典。错误是:期待:逗号,得到:文字:列中的解析错误 28,token \“dd \”(LITERAL),表达式:\ n \“[?a =='cc'&& contains ([u'dd',u'ee'],b)] \“\ n ^ \ n \ n错误 好像一直在 '/home/vagrant/testnew/lieferschein-deployment/stack.yml':第16行, 第6列,但可能在文件的其他位置,具体取决于具体情况 语法问题。\ n \ n违规行似乎是:\ n \ n任务:\ n - debug:\ n ^ here \ n“}
但是,在查询本身中定义列表,内联如"[?a == 'cc' && contains (['dd','ee'],b)]"
,它可以正常工作
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'}] "
}
是否可以将Ansible变量放入查询中,若然,怎么做?
答案 0 :(得分:3)
变化:
bar: ['dd','ee']
为:
bar: "['dd','ee']"
否则它被定义为一个对象,但你想要一个字符串。
如果您已经定义了列表,则需要弯腰检查the documentation,找到合适的过滤器并根据需要进行修改:
将列表连接成字符串:
{{ list | join(" ") }}