Ansible 循环中的set_事实(带有_项)无法按预期工作
我想在循环中向列表中添加一些项。我总是只得到最后一项作为结果Ansible 循环中的set_事实(带有_项)无法按预期工作,ansible,jinja2,Ansible,Jinja2,我想在循环中向列表中添加一些项。我总是只得到最后一项作为结果 $ ansible --version ansible 2.5.1 config file = /home/ansible/ansible_devel/ansible.cfg configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible py
$ ansible --version
ansible 2.5.1
config file = /home/ansible/ansible_devel/ansible.cfg
configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.13 (default, Jan 11 2017, 10:56:06) [GCC]
为了向您说明我的问题所在,我已将代码精简到最低限度:
---
- hosts:
- localhost
become: false
gather_facts: False
roles:
pre_tasks:
tasks:
- name: "Merge firewall variables"
set_fact:
my_list: |
{%- if my_list is not defined -%}
{%- set tmp_my_list=[ 1 ] -%}
{%- else -%}
{%- set tmp_my_list=my_list -%}
{%- endif -%}
{%- set dummy=tmp_my_list.append(item) -%}
{{ tmp_my_list }}
with_items:
- a
- b
- c
- debug: msg="{{ my_list }}"
我所期望的是:
$ ansible-playbook test6.yml
PLAY [localhost] ***************************************************************************************************************************************************************
TASK [Merge firewall variables] ************************************************************************************************************************************************
ok: [localhost] => (item=a)
ok: [localhost] => (item=b)
ok: [localhost] => (item=c)
TASK [debug] *******************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
1,
"a",
"b",
"c"
]
}
PLAY RECAP *********************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
但我得到的是:
$ ansible-playbook test6.yml
PLAY [localhost] ***************************************************************************************************************************************************************
TASK [Merge firewall variables] ************************************************************************************************************************************************
ok: [localhost] => (item=a)
ok: [localhost] => (item=b)
ok: [localhost] => (item=c)
TASK [debug] *******************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
1,
"c"
]
}
PLAY RECAP *********************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
在ansible的旧版本中,此代码起作用。你能帮我在ansible 2.5.1中运行它吗?
谢谢
Lars是一个2.5.1特定的bug,您可以查看更多信息并链接到github问题 升级到ansible 2.5.2将帮助您摆脱它,问题已经解决