Forum Discussion

PK_294685's avatar
PK_294685
Icon for Nimbostratus rankNimbostratus
Jun 16, 2017

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.

 

4 Replies

  • 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 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.

  • rathid's avatar
    rathid
    Icon for Nimbostratus rankNimbostratus

    I am not able to import Managementroot in Python

     

  • 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.