- name: 'Set the foo variable to a combined static list using the Jinja2 syntax'
set_fact:
foo: '{{ [ "one" ] + [ "two", "three" ] }}'
- name: 'Set the foo variable to a combined static list using the Jinja2 syntax and Jinja2 filters'
set_fact:
foo: '{{ [ "one" ] | union([ "two", "three" ]) }}'
5 回答
的确是 . 您需要引用整个数组:
上述任务应生成以下输出:
是的,这是可能的 . 如另一个答案所述,您可以使用双引号设置数组,如下所示:
但是,我想我会创建另一个答案,表明它也可以添加到现有数组中,如下所示:
结合这些并添加调试作为一个剧本(我称之为
facts.yml
),如下所示:生成(通过
ansible-playbook facts.yml
)以下内容:添加到已经给出的答案我想向您展示使用常规YAML语法而不是Ansible核心人员喜欢在其文档中使用的自定义格式来定义
set_fact
列表的不同方法 . 这种自定义格式,就像在这种情况下,已经显示出导致混淆 .考虑这个例子:
直接向前吧?就像在任何普通的YAML文档中一样 . 那么为什么不在Ansible YAML任务文件中使用呢?
关于@ lindes-hw提到的列表组合 . 有不止一种方法可以做到这一点 . 以下示例使用Jinja2语法定义列表:
第二个示例使用
union
过滤器 . 请参阅set theory filters in the Ansible docs .我有类似的要求,根据IP地址列表创建服务器对象列表 .
给出以下输出
我不知道功能是否改变了 - 它可能在2.5 ---但它看起来像我这样的代码
在示例中,您不得不使用它
我正在使用Ansible 2.5.2 . 我得到了这个(不正确的)输出:
我在这里找到了文档中更新的语法:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-items