<p>In my machines , 4 pvc are created. Now i need to get all volume name associated with the pvc in a list. Then those list will be passed to storage array and i will ensure that the volumes are created in storage server. </p>

<pre><code>- name: Verify whether the PVC is created
    command: "kubectl get pvc pvc{{local_uuid}}-{{item}} -o json"
    with_sequence: start=1 end=4
    register: result

- set_fact: 
      pvcstatus: "{{ (item.stdout |from_json).status.phase }}"
      volume_name: "{{(item.stdout |from_json).spec.volumeName}}"
    with_items: "{{ result.results}}"

 - debug: var=volume_name
</code></pre>

<p>But when i run the above tasks , volume_name is having last volumename alone instead of all the volumes as a list. How to get all the volume names in a list?</p>

Samselvaprabu
<p>Your <code>set_fact</code> task is setting <code>volume_name</code> to a single value in each iteration...so of course, when the loop completes, the variable has the value from the final iteration. That's the expected behavior.  If you want a list, you need to create a list.  You can do this by <em>appending</em> to a list in your <code>set_fact</code> loop:</p>

<pre><code>- set_fact: 
    volume_name: "{{ volume_name|default([]) + [(item.stdout |from_json).spec.volumeName] }}"
  with_items: "{{ result.results}}"
</code></pre>

<p>The expression <code>volume_name|default([])</code> will evaluate to an empty list when <code>volume_name</code> is undefined (which is the case on the first iteration of the loop).</p>

<p>I tested this out using the following playbook:</p>

<pre><code>---
- hosts: localhost
  gather_facts: false
  vars:
    result:
      results:
        - stdout: '{"spec": {"volumeName": "volume1"}}'
        - stdout: '{"spec": {"volumeName": "volume2"}}'
        - stdout: '{"spec": {"volumeName": "volume3"}}'
  tasks:
    - debug:
        var: result

    - set_fact: 
        volume_name: "{{ volume_name|default([]) + [(item.stdout |from_json).spec.volumeName] }}"
      with_items: "{{ result.results}}"

    - debug:
        var: volume_name
</code></pre>

<p>Which results in:</p>

<pre><code>TASK [debug] *****************************************************************************************************************************************************************
ok: [localhost] =&gt; {
    "volume_name": [
        "volume1",
        "volume2",
        "volume3"
    ]
}
</code></pre>
