如何在ansible中使用jinja2模板将字典转换为csv



我需要将自定义输出变量转换为字典,然后使用jinja2模板生成csv文件

下面是代码

- 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 }}"

我得到了上面变量"的下面的输出;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"

我尝试使用下面的jinja2模板将该变量转换为csv,但它并没有提供我需要的确切输出。请帮我用csv 完成这件事

{% for key, value in multipath_devices.items() %}
{{key}}
{{value}}
{% endfor %}

我需要像下面一样的CSV文件输出

"device_name","device_id","device_id_status","server_name","server_ip"
name1, 3xxxxxxx3, active, server1, 192.168.56.201

下面是命令输出

name1 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2)
name2 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3)
name3 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4)
name4 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5)
name5 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6)
name6 (3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7)

在files文件夹中创建一个csv.j2文件:(您在与您的剧本相同的位置创建文件夹文件(

{{ multipath_devices.keys()|list }}
{% for i in range(0, (maxnb|max|int)) %}
{%- for k in multipath_devices -%}
{{multipath_devices[k][i] if multipath_devices[k][i] is defined else multipath_devices[k][0]}}{{'' if loop.last else ', '}} 
{%- endfor -%}
{{'n'}}
{%- endfor %}
{%- endfor %}

如果你这样写你的任务:

- 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] }}"

(我模拟你的结果(

- name: "tips1"
hosts: localhost
vars:
multipath_devices:
device_name:
- name1
- name2
- name3
- name4
- name5
- name6
device_id:
- 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2
- 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx3
- 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4
- 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5
- 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6
- 3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7
device_id_status:
- active
- active
- active
- active
- active
- active
server_name: [node-01]          
server_ip: [192.168.56.120]

tasks:
- name: trap max length of all lists
set_fact:
maxnb: "{{ maxnb | d([]) + [item.1|length]}}"
loop: "{{multipath_devices.items()}}"

- name: create csv
template:
src: csv.j2 
dest: result.csv

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

相关内容

  • 没有找到相关文章

最新更新