Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将值分组以键入Ansible_Python_Dictionary_Filter_Ansible_Jinja2 - Fatal编程技术网

Python 将值分组以键入Ansible

Python 将值分组以键入Ansible,python,dictionary,filter,ansible,jinja2,Python,Dictionary,Filter,Ansible,Jinja2,是否有方法将子网4、ipv4地址、ipv4地址优先-ipv4地址最后一个的值分组到密钥“IP” playbook.yml --- - hosts: localhost connection: local gather_facts: False tasks: - name: lookup file set_fact: li: "{{ lookup('file', 'log.yml') | from_yaml }}" - name

是否有方法将子网4ipv4地址ipv4地址优先-ipv4地址最后一个的值分组到密钥“IP

playbook.yml

---
- hosts: localhost
  connection: local
  gather_facts: False
  tasks:
  
  - name: lookup file
    set_fact: 
       li: "{{ lookup('file', 'log.yml') | from_yaml }}"
  - name: run script
    command: 'python3 yaml_to_csv.py'
- comments: FWP - Address Range
  ipv4-address-first: 10.1.2.3
  ipv4-address-last: 10.1.2.5
  ipv4-address: null
  subnet4: null
  subnet-mask: null
  type: address-range
  name: gTest101
- comments: FWP - Host
  ipv4-address-first: null
  ipv4-address-last: null
  ipv4-address: 10.1.2.6
  subnet4: null
  subnet-mask: null
  type: host
  name: gTest102
- comments: FWP - Network
  ipv4-address-first: null
  ipv4-address-last: null
  ipv4-address: null
  subnet4: 41.1.2.3
  subnet-mask: 255.255.255.255
  type: network
  name: gTest103
- comments: FWP - Host 4
  ipv4-address-first: null
  ipv4-address-last: null
  ipv4-address: 10.1.2.7
  subnet4: null
  subnet-mask: null
  type: host
  name: gTest104
log.yml

---
- hosts: localhost
  connection: local
  gather_facts: False
  tasks:
  
  - name: lookup file
    set_fact: 
       li: "{{ lookup('file', 'log.yml') | from_yaml }}"
  - name: run script
    command: 'python3 yaml_to_csv.py'
- comments: FWP - Address Range
  ipv4-address-first: 10.1.2.3
  ipv4-address-last: 10.1.2.5
  ipv4-address: null
  subnet4: null
  subnet-mask: null
  type: address-range
  name: gTest101
- comments: FWP - Host
  ipv4-address-first: null
  ipv4-address-last: null
  ipv4-address: 10.1.2.6
  subnet4: null
  subnet-mask: null
  type: host
  name: gTest102
- comments: FWP - Network
  ipv4-address-first: null
  ipv4-address-last: null
  ipv4-address: null
  subnet4: 41.1.2.3
  subnet-mask: 255.255.255.255
  type: network
  name: gTest103
- comments: FWP - Host 4
  ipv4-address-first: null
  ipv4-address-last: null
  ipv4-address: 10.1.2.7
  subnet4: null
  subnet-mask: null
  type: host
  name: gTest104
这是我调用Ansible来完成这项工作的python代码

yaml\u to\u csv.py

import csv
import yaml

# creating header for the csv file
fields = {
    'name' : 'Name',
    'ipv4-address' : 'IP',
    'subnet4' : 'Subnet4',
    'subnet-mask' : 'Subnet-Mask',
    'ipv4-address-first' : 'IPv4-Address-First',
    'ipv4-address-last' : 'IPv4-Address-Last',
    'comments' : 'Comments',
    'type' : 'Type',
}

# open file and write header data to file
with open('output.csv', 'w', newline='') as f_output:
    csv_output = csv.DictWriter(f_output, fieldnames=fields.values())
    csv_output.writeheader()

    for filename in ['log.yml']:
        with open(filename) as f_input:
            for row_yaml in yaml.safe_load(f_input):
                row_csv = {fields[key] : value for key, value in row_yaml.items()}
                csv_output.writerow(row_csv)
当我运行剧本时

这是我的csv文件输出

output.csv

**下面是我想要实现的预期输出**


是否有我可能遗漏的内容,或者有更好的方法来解决此问题?

是否没有达到您的预期?@mdaniel我从未听说过分组筛选。如何应用?这是否意味着我为您提供的文档链接已断开?无论哪种方式,这都是一个编程网站,因此要显示您尝试的代码,以及它为您产生的错误,以便任何人提供帮助you@mdaniel我正在用一个screenshot@mdaniel我已更新代码以显示最新的输出。