1

[Closed] It is a bug in Ansible v2.5.1, see comment below.

I want to build a new list based on a dictionary. So I try with set_fact and a loop but the variable only contains the last value (not the list)

I try simpler example without dictionary. I use this web site: https://ttl255.com/ansible-appending-to-lists-and-dictionaries/. And it doesn't work has expected.

---
- name: Append to list
  hosts: localhost

  vars:
   devices: []
   cisco:
    - CiscoRouter01
    - CiscoRouter02
    - CiscoRouter03
    - CiscoSwitch01
   arista:
    - AristaSwitch01
    - AristaSwitch02
    - AristaSwitch03

  tasks:

  - name: Add Cisco and Airsta devices to the list
    set_fact:
     devices: "{{ devices + [item] }}"
    with_items:
     - "{{ cisco }}"
     - "{{ arista }}"

  - name: Debug list
    debug:
     var: devices
     verbosity: 0

Extract of the output:

TASK [Debug list] *********************************************************************************************************
ok: [localhost] => {
    "devices": [
        "AristaSwitch03"
    ]
}

Expected:

TASK [Debug list] *********************************************************************************************************
ok: [localhost] => {
    "devices": [
        "CiscoRouter01",
        "CiscoRouter02",
        "CiscoRouter03",
        "CiscoSwitch01",
        "AristaSwitch01",
        "AristaSwitch02",
        "AristaSwitch03"
    ]
}

I use the ansible version: 2.5.1

Crile
  • 23
  • 4
  • 1
    Debug is not the purpose of this. `devices` should contains the concatenation of the two lists, not just the last element. – Crile Jun 18 '19 at 11:45
  • That looked absolutely correct to me so I just ran your above example: I get exactly what you expect (ansible version 2.8.1 - Thanks for providing a reproducilbe example by the way). I then installed ansible 2.5.1 in a virtualenv and I get your above result. This seems to be a bug (didn't take time to look for it). Consider upgrading. – Zeitounator Jun 18 '19 at 12:42
  • 1
    I used to have the same issue with 2.5.1, dont know exactly why and as @Zeitounator said, you can try to upgrade your version. You can also try the union filter: `set_fact: devices: "{{ cisco | union(arista) }}"` – Kelson Silva Jun 18 '19 at 12:54

1 Answers1

0

its a bug of 2.5.1, you will have to upgrade.

check this question for more info.

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