Search code examples
python-3.xrestcsvsdkf5

Python Import CSV to python and Create mutilple VIP on F5


I have to do bulk changes in F5 box using python, I have syntex for creating single Virtual from python, But i want to do it for around 300 +, where inputs are in .CSV with 10 + columns. 300 Rows.

  • So the commands has to fetch the data from row 1 with 10 Data
  • Create the Single VIP in f5 box, then again same function commands has to fetch data from second row
  • if row is empty, it has to stop and give completed status
  • if command failed in any one row, just some message has to place end of the row, then go to next row

Python Command for creating single VIP

myvirtual = bigip.ltm.virtuals.virtual.create(name=["name"],
              description=["description"], 
              destination="%s:%s" % (["ip"], ["port"]), 
              ipProtocol=["ipProtocol"], [pool][1]=["pool"])

CSV format

Row  name   description destination IP  destination Port     ipProtocol pool

1 Server 1 Server 1 172.61.64.1 80 TCp TCp

2 Server 2 Server 2 172.61.64.2 80 TCp TCp

3 Server 3 Server 3 172.61.64.3 80 TCp TCp

4 Server 4 Server 4 172.61.64.4 80 TCp TCp

So, my command has to create 4 Virtual servers from CSV file, each one for one row I am using python 3 and import CSV

please any one help, i am new to python


Solution

  • You can loop through your file using Python's CSV module (Not sure about the syntax for the "bigip" module, but you can debug from the error on the console, if any). Reading every row ("vs") as a dictionary would allow you to reference every field by the column name:

    import csv
    with open('path-to-your-file.csv', 'rb') as f:
        for vs in csv.DictReader(f):
            try:
                bigip.ltm.virtuals.virtual.create(
                    name=vs['name'],
                    description=vs['description'],
                    destination="%s:%s" % (vs['destination IP'], vs['destination Port']), 
                    ipProtocol=vs['ipProtocol'],
                    pool=vs['pool']
                )
            except Exception as e:
                print e