简体   繁体   中英

How to convert dictionary to csv using jinja2 templates in ansible?

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)

create a csv.j2 file in files folder: (you create folder files in same place than your playbook)

"device_name","device_id","device_id_status","server_name","server_ip"
{% for devicename in multipath_devices.device_name %}
{{devicename}}, {{multipath_devices.device_id[loop.index0]}}, {{multipath_devices.device_id_status[loop.index0]}}, {{servername}}, {{serverip}}
{% endfor %}

the playbook to use it:

- name: "tips1"
  hosts: localhost
  vars:
    multipath_devices:
      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    
  tasks:  
    - name: create csv
      #delegate_to: localhost
      template:
        src: csv.j2   #cherche dans templates
        dest: result.csv
      vars:
        servername: "{{ multipath_devices.server_name }}"
        serverip: "{{ multipath_devices.server_ip}}"
    

result in result.csv:

"device_name","device_id","device_id_status","server_name","server_ip"
name1, 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2, active, node-01, 192.168.56.120
name2, 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3, active, node-01, 192.168.56.120
name3, 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4, active, node-01, 192.168.56.120
name4, 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5, active, node-01, 192.168.56.120
name5, 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6, active, node-01, 192.168.56.120
name6, 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7, active, node-01, 192.168.56.120

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM