Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

F5 iControl & Python for Beginners

Folks,

There's been lot of iControl Rest and Python stuff going on. I recently downloaded Python SDK from here https://github.com/F5Networks/f5-common-python. I have downloaded python 2.7 and 3.6 as well. I'm running v12.1.2 on my f5 appliances.

FYI, I'm a fresher when it comes to Python.

Now the silly question, What do i do?

What do i do before i run a python script?

Do i need to copy the SDK folder on to Python installed folder?

All I'm looking for is a way to integrate F5 Python SDK and Python to run my 1st script.

Any help is appreciated! Thanks in advance!

Hope this helps new folks trying to run python on F5.

1
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

So...wow. You're asking for quite a mouthful in one question! I'll try to cover some basics to get you started but there are probably much better resources on DevCentral and outside with some searches on F5 and Python.

I believe the SDK is still Python2. I have successfully used Python3 for some simple stuff, but you'll encounter problems with some portions. Stick with Python2 for now when using the SDK. (See Issue 287 at the GitHub page)

You do need to 'install' the SDK so the modules end up in the correct directories for Python. Once you get comfortable with Python a bit more, I highly recommend virtual environments as you progress, but don't worry about it for this. One easy way to install the SDK is using pip. You may already have pip if your version is >2.7.9. Once you have pip installed, install the SDK with:

pip install f5-sdk

Pip takes care of dependency packages for you. After that you should see 'Successfully installed...' and a list of packages including f5-sdk-x.x.x. A quick test to ensure the SDK is installed is to start a Python interpreter and try importing the ManagementRoot module. In the section below, I start a Python interpreter session, import the ManagementRoot class from f5.bigip module, then use the builtin dir function to list the attributes in the class. It just confirms the module was installed properly and is available for use.

(f5p2) ed@block:~/python$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from f5.bigip import ManagementRoot
>>> dir(ManagementRoot)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_build_final_uri', '_check_command_parameters', '_check_exclusive_parameters', '_check_force_arg', '_check_generation', '_check_supported_versions', '_format_collection_name', '_format_resource_name', '_get_base_uri', '_get_icr_session', '_get_tmos_version', '_handle_requests_params', '_is_version_supported_method', '_set_meta_data_uri', 'configure_meta_data', 'hostname', 'icontrol_version', 'parse_arguments', 'post_configuration_setup', 'raw', 'set_icr_metadata', 'set_metadata_uri', 'tmos_version', 'transform_attr_names']
>>> 

Now you can have some fun. Try some of the sample scripts from the F5 Python SDK documentation. You can simply type the commands into your interpreter session for now. Go through the docs to get familiar with the structure - which is somewhat intuitive if you're accustomed to using TMSH.

Quick example: using the same interpreter session above I establish a session to a BIG-IP, collect all pools in a variable, then cycle through each pool and print it's name to screen. In this example, the management IP is 10.1.0.1 and user/pass is admin/admin.

>>> mgmt = ManagementRoot('10.1.0.1', 'admin', 'admin')
>>> my_pools = mgmt.tm.ltm.pools.get_collection()
>>> for pool in my_pools:
...     print(pool.name)
... 
another_pool
pool_1
>>> 

Difficult to cover as the possibilities are so open-ended. Suggest trying above to dip your toe in the water, and reviewing some of the links. Reach out if you get stuck on a particular issue. DevCentral has been a fantastic resource (both articles and community assistance) so feel free to holler if you hit any specific issues.

1
Comments on this Answer
Comment made 17-Jun-2017 by Ed Summers 1106

One comment on this...You'll likely run into urllib3 'InsecureRequestWarning' warnings when the REST calls are made to your BIG-IP. This is from the urllib3 module which is used by the SDK. It's complaining that the BIG-IP device certificate isn't trusted.

One suggestion in Issue 1093 may work. You can use the snippet below in your script to disable these warnings (understanding the security risk...that you're not verifying the device to which you are communicating). Ensure other mitigations are utilized such as an isolated management network.

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
1
Comment made 17-Jun-2017 by Ryan 860

Nice explanation Ed.

0
Comment made 20-Jun-2017 by PK 681

Ed-
You did a great job in your answer and i appreciate you taking time to explain.

Now,
When I try to run the SDK setup, i'm getting errors regarding requirements. How can i know if i'm fulfilling the below requirements of SDK?
# F5-SDK base install, python requirements
six>=1.9.0
six<2.0.0
f5-icontrol-rest>=1.3.0
f5-icontrol-rest<2.0.0

Thanks in advance!

0
Comment made 20-Jun-2017 by Ed Summers 1106

How are you attempting to install the SDK? Are you using pip or another method? I highly recommend using pip as it should automatically determine and download dependencies for you. In my original example I tested this by creating a new python virtualenv and running:

pip install f5-sdk

That's also the recommended install method from the documentation. If you install it 'manually' you'll need to first install all of the dependencies. Those are usually listed on PyPi but I didn't immediately see them for that package. If you have the package uncompressed you can check for a requirements.txt file that will list the required modules and versions. Each one can be downloaded from PyPi and installed.

Try pip first (basically ignore the file you downloaded and run the pip command. Let pip download and install). If we can't get that to work or you absolutely cannot use pip for some reason, you can download the required packages listed from PyPi, install them, then try installing the SDK.

0
Comment made 20-Jun-2017 by PK 681

Ed- Thanks for the response.

I tried installing SDK from Python IDLE Shell but getting "invalid syntax" error. Where would i actually install?
I have python installed on C: drive and i have SDK uncompressed and copied to C: drive as well. Python & SDK are in different folder in C: drive.
What would be my next steps?

0
Comment made 20-Jun-2017 by Ed Summers 1106

My bad - I was assuming a Linux system.

Pip is normally run from a system command line (not the python interpreter/shell). (E.g. C:\ prompt)

Did a quick search on python/pip on Windows and found that typically the path to the pip executable (pip.exe) is not included in the system executable PATH. Make sure that's added to your path. (Check this link and scroll down to the March 2016 Update post.) Once you get pip in your path you should be able to run the pip install f5-sdk command above to get the SDK and dependencies installed. This doc may provide some additional information on getting pip running and installing packages.

0
Comment made 20-Jun-2017 by PK 681

Awesome i will give it a try & update the post.. Thanks again Ed

0
Comment made 20-Jun-2017 by PK 681

Cool.. So I ran this c:\Python27\Scripts>pip install f5-sdk and it successfully installed all dependencies needed for f5-sdk.

I'm able run my 1st python script from Python CLI..

Ed- you are awesome.. I did get the "InsecureRequestWarning" and i imported those packages to ignore.

Worked like a charm!

Thanks again for your help with this. Hope this helps newbies out there..

0
Comment made 20-Jun-2017 by Ed Summers 1106

Fantastic! Glad it is working! Have fun and reach out if any questions. I have gotten some great help from community on the SDK.

0
Comment made 2 months ago by Mohammed M Irfan 116

Hi PK,

I tried, but i am getting an error

C:\>pip install f5-sdk

No Perl script found in input
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

I am not able to import Managementroot in Python

0
Comments on this Answer
Comment made 29-Jul-2017 by Ed Summers 1106

Hi rathid! You may want to start a new question to better keep track of the answer thread.

Regardless, can you post the exact error you receive when you try to import ManagementRoot (note: it is case sensitive)? Did you install the F5 SDK successfully?

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Hi, I was reading this post and created new post for error getting while doing same thing.

Here is new post. please let me know if anyone can help.

https://devcentral.f5.com/questions/while-connecting-to-f5-sdk-error-58943

Thanks AN

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

I do not recommend you to use the F5-SDK in fact recommend you use the request module to call http-get and http-post with json data. I realize the F5 SDK has some limitation in choosing fasthttp and fastL4 profile for VS, these two profiles have no attributes so i think in order to modify the VS with the performance L4 or performance http using json and do a http-post will be better.

0