I want to add some items to a list in a loop. Always I get only the last item as result.
$ 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]
To show you what my problem is I have stripped down the code to the minimum:
---
- 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 }}"
What I would expect is:
$ 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
But what I get is:
$ 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
In older versions of ansible this code worked. Can you help me to get it running in ansible 2.5.1? Thanks, Lars