[python]Creating bigip pool and pool members with F5 icontrol

I have recently downloaded the F5 SDK which helped to use the iControl REST API easily, it has a documentation here: https://f5-sdk.readthedocs.io, there are some coding example too, however not too in depth. So i started by just trying to create a new pool and iterate the pool members. This is a sample code to show it’s working, codes for checking if pool and pool members exist are required.

I have added some logging in points of event, and I must say when you start to script and have no idea where is the problem, putting loggers on points of events will help you troubleshoot and find out which point the script stops working.

Here’s the sample code:

from f5.bigip import ManagementRoot
import logging


logging.basicConfig(filename="bigip_script.log",level=logging.INFO)
poolMembers = list() # collects user input for pool members.
poolMember = '1' # initialization
poolName = input("Enter pool name: ")
print("\n")
lbMode = input("Enter load balancing method ")
print("\n")
while poolMember is not '0':
    poolMember = input("Enter member in this format ip_address:port type 0 to finish ") # creating nodes until user stops
    print("\n")
    poolMembers.append(poolMember)

try:
    mgmt = ManagementRoot("192.168.1.11","admin","p@ssw0rd") # connection to bigip
except Exception as e:
    logging.error(e)
    print(e)
    exit(1)

ltm = mgmt.tm.ltm
try:
    pool1 = ltm.pools.pool.create(name=poolName,partition="Common",loadBalancingMode=lbMode)
    logging.info("Creating pool {} with load balancing method as {}".format(poolName,lbMode))
except Exception as e:
    logging.error(e)
    print(e)
    exit(1)

for member in poolMembers:
    if member is '0': #exclude exit code, 0 is to exit the member prompt.
        logging.info("End of the pool members, exiting...")
        break

    try:
        pool1.members_s.members.create(name=member,partition="Common")
        logging.info("Creating member {}...".format(member))
    except Exception as e:
        logging.error(e)
        print(e)
        exit(1)

The logs that told me the code was successful:

INFO:root:Creating pool pool-Gwenpool with load balancing method as round-robin
INFO:root:Creating member 1.1.1.1:80...
INFO:root:Creating member 2.2.2.2:80...
INFO:root:End of the pool members, exiting...
INFO:root:Creating pool pool-Deadpool with load balancing method as least-connections-member
INFO:root:Creating member 192.168.1.100:1433...
INFO:root:Creating member 192.168.20.1:1433...
INFO:root:Creating member 192.168.30.1:1433...
INFO:root:End of the pool members, exiting...

Advertisements
This entry was posted in Python, Scripting and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s