Ansible:条件复杂

时间:2017-01-18 10:20:39

标签: python ansible ansible-playbook

我想在我的剧本中写下这段代码:

- debug:
    msg: hello
  when: '2 in [x*2 for x in range(1, 10)]'

但我有一个错误:

The conditional check '2 in [x*2 for x in range(1, 10)]' failed.
The error was: template error while templating string: expected token ',',
got 'for'.
String: {% if 2 in [x*2 for x in range(1, 10)] %} True {% else %} False 

我该如何重写它?我想使用生成器来处理with_items的复杂条件。这是python示例:

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
>>> 2 in [x*2 for x in range(1, 10)]
True

1 个答案:

答案 0 :(得分:0)

“when”值是jinja2。文档链接到支持的比较(http://jinja.pocoo.org/docs/dev/templates/#comparisons

对于您的实际示例,支持此语法:

when: '2 in [2, 4, 6, 8, 10, 12, 14, 16, 18]'