I could not comment on the same question here, not enough Republic credits
I attempted the yaml cmd as suggested by @Anthon but does not shield the desired output and have no idea how to start modifying the yaml from-csv code, I did look into the code in "yaml_cmd.py"
Any guidance would be appreciated. Trying to convert a large csv file into hosts.yaml format to use with 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
Well, this is not pretty but worked
# 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))
Output is:
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