我无法对同样的问题发表评论这里,没有足够的共和国学分
我按照@Anthon的建议尝试了yaml cmd,但没有屏蔽所需的输出,并且不知道如何开始修改yaml from-csv代码,我确实查看了“yaml_cmd.py”中的代码
任何指导将不胜感激。尝试将大型 csv 文件转换为hosts.yaml 格式以与 Nornir 一起使用。
Display-Name,IP-Address,Serial-Number,Machine-Type,IOS-Version,SiteCode2,Group
Device1,1.1.1.1,123456790,Cisco,12.x,Site1,US
Device2,1.1.1.2,123456789,Cisco,13.x,Site2,US
- "\uFEFFDisplay-Name"
- IP-Address
- Serial-Number
- Machine-Type
- IOS-Version
- SiteCode2
- Product-Edition
- Product-Version
- - Device1
- 1.1.1.1
- 123456790
- Cisco
- 12.x
- Site1
- US
- - Device2
- 1.1.1.2
- 123456789
- Cisco
- 13.x
- Site2
- US
Device1:
hostname: 1.1.1.1
platform: Cisco
groups:
- US
data:
site: Site1
SN: 123456790
version: 12.x
Device2:
hostname: 1.1.1.2
platform: Cisco
groups:
- US
data:
site: Site2
SN: 123456789
version: 13.x
嗯,这不太漂亮,但很有效
# Import the csv library
import csv
# Open the sample csv file and print it to screen
with open("test-file.csv") as f:
print (f.read())
# Open the sample csv file, and create a csv.reader object
with open("test-file.csv") as f:
csv_2_yaml = csv.reader(f)
# Loop over each row in csv and leverage the data in yaml
for row in csv_2_yaml:
device = row[0]
ip = row[1]
SN = row[2]
platform = row[3]
version = row[4]
site = row[5]
group = row[6]
print ("{0}:\n hostname: {1}\n platform: {2}\n group:\n - {3}\n data:\n site: {4}\n SN: {5}\n version: {6}\n"
.format(device, ip, platform, group, site, SN, version))
输出是:
Display-Name,IP-Address,Serial-Number,Machine-Type,IOS-Version,SiteCode2,Group
Device1,1.1.1.1,123456790,Cisco,12.x,Site1,US
Device2,1.1.1.2,123456789,Cisco,13.x,Site2,US
Display-Name:
hostname: IP-Address
platform: Machine-Type
group:
- Group
data:
site: SiteCode2
SN: Serial-Number
version: IOS-Version
Device1:
hostname: 1.1.1.1
platform: Cisco
group:
- US
data:
site: Site1
SN: 123456790
version: 12.x
Device2:
hostname: 1.1.1.2
platform: Cisco
group:
- US
data:
site: Site2
SN: 123456789
version: 13.x