F5 Friday: Python SDK for BIG-IP
We know programmability is important. Whether we’re talking about networking and SDN, or DevOps and APIs and templates, the most impactful technologies and trends today are those involving programmability.
F5 is, as you’re no doubt aware, no stranger to programmability. Since 2001 when we introduced iControl (API) and iRules (data path programmability) we’ve continued to improve, enhance, and expand the ability of partners, customers, and our own engineers and architects to programmatically control and redefine the application delivery experience.
With the emphasis today on automation and orchestration as a means for ops (and through it, the business) to scale more quickly and efficiently, programmability has never before been so critical to both operational and business success.
Which means we can’t stop improving and expanding the ways in which you (and us, too) can manage, extend, and deliver the app services everyone needs to keep their apps secure, fast, and available.
Now, iControl and iControl REST are both APIs built on open standards like SOAP, JSON, and HTTP. That means anyone who knows how to use an API can sit down and start coding up scripts that automate provisioning, configuration, and general management of not just BIG-IP (the platform) but the app services that get deployed on that platform.
And we’re not against that at all. But we also recognize that not everyone has the time to get intimately familiar with iControl in either of its forms. So we’re pretty much always actively developing new (and improving existing) software development kits (SDKs) that enable folks to start doing more faster. But so are you. We’ve got a metric ton of code samples, libraries, and solutions here on DevCentral that have been developed by customers and partners alike. They’re freely available and are being updated, optimized, extended and re-used every single day. We think that’s a big part of what an open community is – it’s about developing and sharing solutions to some of the industry’s greatest challenges.
And that’s what brings us to today’s exciting news. Well, exciting if you’re a Python user, at least, because we’re happy to point out the availability of the F5 BIG-IP Python SDK. And not just available to download and use, but available as an open source project that you can actively add, enhance, fork, and improve. Because open source and open communities produce some amazing things.
This project implements an SDK for the iControl REST interface for BIG-IP, which lets you create, edit, update, and delete (CRUD) configuration objects on a BIG-IP. Documentation is up to date and available here.
The BIG-IP Python SDK layers an object model over the API and makes it simpler to develop scripts or integrate with other Python-based frameworks. The abstraction is nice (and I say that with my developer hat on) and certainly makes the code more readable (and maintainable, one would assume) which should help eliminate some of the technical debt that’s incurred whenever you write software, including operational scripts and software.
Seriously, here’s a basic sample from the documentation:
from f5.bigip import BigIP # Connect to the BigIP bigip = BigIP("bigip.example.com", "admin", "somepassword") # Get a list of all pools on the BigIP and print their name and their # members' name pools = bigip.ltm.pools.get_collection() for pool in pools: print pool.name for member in pool.members: print member.name # Create a new pool on the BigIP mypool = bigip.ltm.pools.pool.create(name='mypool', partition='Common') # Load an existing pool and update its description pool_a = bigip.ltm.pools.pool.load(name='mypool', partition='Common') pool_a.description = "New description" pool_a.update() # Delete a pool if it exists if bigip.ltm.pools.pool.exists(name='mypool', partition='Common'): pool_b = bigip.ltm.pools.pool.load(name='oldpool', partition='Common') pool_b.delete()
Isn’t that nice? Neat, understandable, readable. That’s some nice code right there (and I’m not even a Python fan, so that’s saying something). Don’t let the OpenStack reference fool you. While the first “user” of the SDK is OpenStack, it is stand-alone and can be used on its own or incorporated into other Python-based frameworks.
So if you’re using Python (or were thinking about) to manage, manipulate, or monitor your BIG-IPs, check this one out. Use it, extend it, improve it, and share it.
Happy scripting!
- Eric_ChenEmployee
Updated example. Previous example from the article reflects an earlier release of the F5 Python SDK:
from f5.bigip import ManagementRoot Connect to the BigIP mgmt = ManagementRoot("bigip.example.com", "admin", "somepassword") Get a list of all pools on the BigIP and print their names and their members' names pools = mgmt.tm.ltm.pools.get_collection() for pool in pools: print pool.name for member in pool.members_s.get_collection(): print member.name Create a new pool on the BIG-IP mypool = mgmt.tm.ltm.pools.pool.create(name='mypool', partition='Common') Load an existing pool and update its description pool_a = mgmt.tm.ltm.pools.pool.load(name='mypool', partition='Common') pool_a.description = "New description" pool_a.update() Delete a pool if it exists if mgmt.tm.ltm.pools.pool.exists(name='mypool', partition='Common'): pool_b = mgmt.tm.ltm.pools.pool.load(name='mypool', partition='Common') pool_b.delete()
David:
Try adding these lines:
from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
- Great to see. Will BIG-IQ get a similar SDK?
- JRahmAdminNot a python fan?!? We might not be able to be friends any longer...I'll consult myself over the weekend and let you know.
- thanks for sharing
- does it work with python 3?
- JGCumulonimbus
I have just tried some of this code, but got the following error:
Traceback (most recent call last): File "./f5_icontrolREST_test.py", line 20, in for member in pool.members: File "/usr/local/lib/python2.7/dist-packages/f5/bigip/mixins.py", line 82, in __getattr__ raise AttributeError(error_message) AttributeError: '' object has no attribute 'members'
and I am pretty sure that pool has a member defined. I am on v11.6.1.
- JGCumulonimbus
I have found out what went wrong now. This works:
pools = mgmt.tm.ltm.pools.get_collection() for pool in pools: print pool.name for member in pool.members_s.get_collection(): print member.name
- Hannes_RappNimbostratus
@Sebastian. You can use it with Python 3. (Have tested with Py 3.4.3)
"pip3 install f5-sdk"
- michaelc0n_6121Nimbostratus
What about py 2.7 support? (Just asking ) thx!