0

Why is new_list not being concatenated too?

---
- hosts: localhost
  gather_facts: False

  tasks:     
  - name: Set init list
    set_fact:
      init_list:
       - 1
       - 2
       - 3

  - name: New list from with_items
    set_fact:
      new_list: "{{ new_list|default([]) + [ item ] }}"
    with_items: "{{ init_list }}"

  - debug:
      var: new_list

When I run this short script I get the following output:

TASK [Set init list] 
*************************************************************************
ok: [localhost]

TASK [New list from with_items] 
*************************************************************************
ok: [localhost] => (item=1)
ok: [localhost] => (item=2)
ok: [localhost] => (item=3)

TASK [debug] 
*************************************************************************
ok: [localhost] => {
    "new_list": [
        3
    ]
}

I am expecting new_list to contain 1,2,3. The concatenation is working but overwriting each time. I was sure this was working before (Ubuntu 16.04, unsure of Ansible version) and I am wondering if this is an Ansible version issue.

# Ubuntu 18.04
# Ansible --version
ansible 2.5.1
  python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]

Note: I know there are other jinja2 filters that could potentially work around this e.g. map, list, etc, but they are not easily applicable in my production code.

Martin
  • 2,316
  • 1
  • 28
  • 33
  • I found https://stackoverflow.com/questions/29399581/using-set-facts-and-with-items-together-in-ansible and the answer from stacyhorton shows exactly this approach working. I'm going to try v2.2. – Martin May 29 '19 at 15:00

1 Answers1

0

I have finally found the correct answer. This is broken in Ansible version 2.5.1 which is unfortunately the version that comes by default in Ubuntu 18.04.

The current latest (2.5.5) does not have the issue.

sudo -H pip install ansible==2.5.5

Martin
  • 2,316
  • 1
  • 28
  • 33