1

take this playbook for example:

---
- hosts: localhost
  gather_facts: no
  vars:
    in_list:
      - value1
      - value2
      - value3
    final_list: []

  tasks:

    - debug:
        var: in_list

    - name: parse list
      set_fact:
        final_list: "{{ final_list + [{'key': item}] }}"
      with_items: "{{ in_list }}"

    - debug:
        var: final_list

it seems that the final_list is replaced on each iteration by the last set_fact replacement, i.e its not appending to it on each loop.

output:

[root@optima-ansible ILIAS]# ansible-playbook append_to_list.yml 

PLAY [localhost] ****************************************************************************************************************************************************************************************************

TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "in_list": [
        "value1", 
        "value2", 
        "value3"
    ]
}

TASK [parse list] ***************************************************************************************************************************************************************************************************
ok: [localhost] => (item=value1)
ok: [localhost] => (item=value2)
ok: [localhost] => (item=value3)

TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "final_list": [
        {
            "key": "value3"
        }
    ]
}

PLAY RECAP **********************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0   

[root@optima-ansible ILIAS]# 

i used code from this question

what am i doing wrong?

update: my setup:

[root@optima-ansible ILIAS]# ansible --version
ransible 2.5.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.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.14 (default, Mar 14 2018, 13:36:31) [GCC 7.3.1 20180303 (Red Hat 7.3.1-5)]
[root@optima-ansible ILIAS]# rpm -qa --last |  grep ansible
ansible-2.5.1-1.fc27.noarch                   Sun 22 Apr 2018 02:46:30 AM EEST
[root@optima-ansible ILIAS]# 
ilias-sp
  • 6,135
  • 4
  • 28
  • 41
  • even code from [this tutorial](http://ttl255.com/ansible-appending-to-lists-and-dictionaries/) doesnt work in my ansible as demonstrated in the page, feel free to try the `append_dict_v2.yml` – ilias-sp Apr 23 '18 at 14:11
  • you mean its bug of my ansible installation? i am using the one shipping with fedora, its 2.5.1 adding to the question for better visibility – ilias-sp Apr 23 '18 at 14:19
  • Then post an issue on GitHub, as all the other versions except for 2.5.1 produce your expected results. – techraf Apr 23 '18 at 14:22
  • You are right. using the ansible-2.5.0-1.el7.ans.noarch.rpm the code works as expected. and its not distribution issue, the problem is reproducable using the 2.5.1 from [here](https://releases.ansible.com/ansible/rpm/release/epel-7-x86_64/). i will open issue and downgrade ansible to continue my work i guess :) thank you – ilias-sp Apr 23 '18 at 14:43
  • 1
    I had this same exact problem with 2.5.1; updating to latest resolved it – modle13 Aug 22 '20 at 02:21

1 Answers1

4

adding an answer to close the thread, with the official response i got from the Ansible team on the issue i opened at Github. Obviously this was already known to them, they closed my issue as duplicate redirecting me to these 2:

#38302 and #38075

ilias-sp
  • 6,135
  • 4
  • 28
  • 41