copy list of set_fact output in csv column - ansible

Viewed 30

i got the output with set_fact as below

"my_var": [
    [
        " iDRAC.Embedded.1",
        " NIC.Integrated.1-1-1",
        " NIC.Integrated.1-2-1",
        " NIC.Integrated.1-3-1",
        " NIC.Integrated.1-4-1",
        " NIC.Slot.4-1-1",
        " NIC.Slot.4-2-1",
        " NIC.Slot.7-1-1",
        " NIC.Slot.7-2-1",
        " NIC.Slot.1-1-1",
        " NIC.Slot.1-2-1",
        " NIC.Slot.2-1-1",
        " NIC.Slot.2-2-1"
    ],
    [
        " ABC",
        " Not Supported",
        " No Link",
        " No Link",
        " No Link",
        " XYZ",
        " XYZ",
        " XYZ",
        " XYZ",
        " XYZ",
        " XYZ",
        " XYZ",
        " XYZ"
    ]
]

}

now, i want this to be updated in csv file in column. for Example iDRAC.Embedded.1 in first column and ABC in next column. so on and so forth

1 Answers

For example

    - copy:
        dest: my_var.csv
        content: |-
          {% for line in csv %}
          {{ line }}
          {% endfor %}
      vars:
        csv: "{{ my_var.0|map('trim')|
                 zip(my_var.1|map('trim'))|
                 map('join', ',')|
                 list }}"

gives

shell> cat my_var.csv 
iDRAC.Embedded.1,ABC
NIC.Integrated.1-1-1,Not Supported
NIC.Integrated.1-2-1,No Link
NIC.Integrated.1-3-1,No Link
NIC.Integrated.1-4-1,No Link
NIC.Slot.4-1-1,XYZ
NIC.Slot.4-2-1,XYZ
NIC.Slot.7-1-1,XYZ
NIC.Slot.7-2-1,XYZ
NIC.Slot.1-1-1,XYZ
NIC.Slot.1-2-1,XYZ
NIC.Slot.2-1-1,XYZ
NIC.Slot.2-2-1,XYZ
Related