Home > Mobile >  Ansible concatenate list with loop - missing element
Ansible concatenate list with loop - missing element

Time:02-01

I try to concatenate list of list with Ansible. It works with my loop, except the last element is missing in my final list. I retrieve the list from the volumes_list variable and then concatenate volumes_list with each retrieved list with the contents of volumes_list. But the last concatenation is missing. The code:

  any_errors_fatal: true
  max_fail_percentage: 0

  vars_files:
    - vars/{{ env }}.yml

  tasks:
    - name: Retrieve array info
      purestorage.flasharray.purefa_info:
        gather_subset:
          - volumes
          - hosts
        fa_url: "{{ src_fa_url }}"
        api_token: "{{ src_fa_api_token }}"
      register: array_info

    - name: Get list of volumes to eradicate
      vars:
        volumelist: []
        volumes_list: []

      set_fact:
          volumes_list: "{{ array_info.purefa_info.hosts[item].volumes | map(attribute='volume') | list }}"
          volumelist: "{{ volumelist    volumes_list }}"
      with_items: "{{ src_host_name }}"

    - debug:
        var: volumelist

What the volumelist should be:

TASK [debug] ***********************************************************************************************************************************************************************************************
task path: /data/demo-k8s/ansible-deploy-vmware-vm/HA/mygather.yml:36
ok: [localhost] => {
    "volumelist": [
        "democenter-ha-dc1-worker01_pwxvol-01",
        "democenter-ha-dc1-worker02_pwxvol-01",
        "democenter-ha-dc2-worker03_pwxvol-01",
        "democenter-ha-dc2-worker04_pwxvol-01",
        "democenter-ha-dc3-worker05_pwxvol-01",
        "democenter-ha-dc3-worker06_pwxvol-01"
    ]
}

And what I get:

TASK [debug] ***********************************************************************************************************************************************************************************************
task path: /data/demo-k8s/ansible-deploy-vmware-vm/HA/gather.yml:37
ok: [localhost] => {
    "volumelist": [
        "democenter-ha-dc1-worker01_pwxvol-01",
        "democenter-ha-dc1-worker02_pwxvol-01",
        "democenter-ha-dc2-worker03_pwxvol-01",
        "democenter-ha-dc2-worker04_pwxvol-01",
        "democenter-ha-dc3-worker05_pwxvol-01"

    ]
}

CodePudding user response:

Check if you have all hosts in this var: {{ src_host_name }} –

CodePudding user response:

Yes I got all hosts in the var {{ src_host_name }}.

If I trace, the last loop is:

ok: [localhost] => (item=democenter-ha-dc3-worker06) => {
    "ansible_facts": {
        "volumelist": [
            "democenter-ha-dc1-worker01_pwxvol-01",
            "democenter-ha-dc1-worker02_pwxvol-01",
            "democenter-ha-dc2-worker03_pwxvol-01",
            "democenter-ha-dc2-worker04_pwxvol-01",
            "democenter-ha-dc3-worker05_pwxvol-01"
        ],
        "volumes_list": [
            "democenter-ha-dc3-worker06_pwxvol-01"
        ]
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": "democenter-ha-dc3-worker06"
}
Read vars_file 'vars/{{ env }}.yml'

TASK [debug] ***********************************************************************************************************************************************************************************************
task path: /data/demo-k8s/ansible-deploy-vmware-vm/HA/gather.yml:37
ok: [localhost] => {
    "volumelist": [
        "democenter-ha-dc1-worker01_pwxvol-01",
        "democenter-ha-dc1-worker02_pwxvol-01",
        "democenter-ha-dc2-worker03_pwxvol-01",
        "democenter-ha-dc2-worker04_pwxvol-01",
        "democenter-ha-dc3-worker05_pwxvol-01"
    ]
}
  •  Tags:  
  • Related