i need to convert the custom output variable to convert into dictionary then to generate csv file using jinja2 template
below is the code
- name: Executing the cmd (multipath -ll | grep dm- | awk -F' dm' '{print $1}') to get the device name
shell: multipath -ll | grep dm- | awk -F' dm' '{print $1}' | awk -F ' ' '{print $1}'
register: multipath_device_name
- name: Executing the cmd (multipath -ll | grep -E status='(active|inactive)' | awk -F ' ' '{print $5}' | cut -c 8-16) to get the device active or inactive
shell: multipath -ll | grep -E status='(active|inactive)' | awk -F ' ' '{print $5}' | cut -c 8-16
register: multipath_device_status
- name: Executing the cmd (multipath -ll | grep dm- | awk -F' dm' '{print $1}' | awk -F ' ' '{print $2}' | cut -d "(" -f2 | cut -d ")" -f1) to get the device active or inactive
shell: multipath -ll | grep dm- | awk -F' dm' '{print $1}' | awk -F ' ' '{print $2}' | cut -d "(" -f2 | cut -d ")" -f1
register: multipath_device_id
- set_fact:
multipath_devices:
device_name: "{{ multipath_device_name.stdout_lines }}"
device_id: "{{ multipath_device_id.stdout_lines }}"
device_id_status: "{{ multipath_device_status.stdout_lines }}"
server_name: "{{ ansible_hostname }}"
server_ip: "{{ ansible_default_ipv4.address }}"
- debug:
msg: "{{ multipath_devices }}"
I got the below output for the above variable "multipath_devices"
"msg": {
"device_id": [
"3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2",
"3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3",
"3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4",
"3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5",
"3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6",
"3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7",
],
"device_id_status": [
"active",
"active",
"active",
"active",
"active",
"active"
],
"device_name": [
"name1",
"name2",
"name3",
"name4",
"name5",
"name6"
],
"server_ip": "192.168.56.120",
"server_name": "node-01"
I tried the above to convert that variable into csv using jinja2 template like below, its not giving the exact output what i need. please help me to get out this done with csv
{% for key, value in multipath_devices.items() %}
{{key}}
{{value}}
{% endfor %}
I need the CSV file output like below
"device_name","device_id","device_id_status","server_name","server_ip"
name1, 3xxxxxxx3, active, server1, 192.168.56.201
below is the command output
name1 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2)
name2 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3)
name3 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4)
name4 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5)
name5 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6)
name6 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7)