Forum Discussion
Need help on F5 automation to read the irules attached t all the virtual servers
- Aug 20, 2017
This is tested with Python 3.4.3 and BigIP 12.1. Did not have 12.0 installation readily available, but it should work the same for you
from f5.bigip import ManagementRoot Optional: omit unsecure certificate warnings from output import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) API_ROOT = ManagementRoot("bip-01", "admin", "admin") VSERVER_COLLECTION = API_ROOT.tm.ltm.virtuals.get_collection() for vserver in VSERVER_COLLECTION: print(vserver.name) Catch an error to avoid a VS with no iRules breaking the script prematurely try: irules = vserver.rules except AttributeError: print('- No iRules on this Virtual Server!') else: for rule in vserver.rules: print('-', rule)
Output in test env:
$ python list_irules.py vs_0.0.0.0_any - No iRules on this Virtual Server! vs_test_80 - /Common/ir_do_nothing - /Common/ir_do_nothing_again vs_test_443 - /Common/ir_do_nothing - /Common/ir_do_nothing_again
As you can see from the script above, this takes a bit of testing to get the iRules listed. Ofcourse, ideally either the API or SDK should return an empty list if there are no iRules on a Virtual Server, rather than a script-breaking "AttributeError: 'f5.bigip.tm.ltm.virtual.Virtual'>' object has no attribute 'rules'". SDK is a build up on top of a problematic API, so eventually this problem should be addressed by the API developers.
This is tested with Python 3.4.3 and BigIP 12.1. Did not have 12.0 installation readily available, but it should work the same for you
from f5.bigip import ManagementRoot
Optional: omit unsecure certificate warnings from output
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
API_ROOT = ManagementRoot("bip-01", "admin", "admin")
VSERVER_COLLECTION = API_ROOT.tm.ltm.virtuals.get_collection()
for vserver in VSERVER_COLLECTION:
print(vserver.name)
Catch an error to avoid a VS with no iRules breaking the script prematurely
try:
irules = vserver.rules
except AttributeError:
print('- No iRules on this Virtual Server!')
else:
for rule in vserver.rules:
print('-', rule)
Output in test env:
$ python list_irules.py
vs_0.0.0.0_any
- No iRules on this Virtual Server!
vs_test_80
- /Common/ir_do_nothing
- /Common/ir_do_nothing_again
vs_test_443
- /Common/ir_do_nothing
- /Common/ir_do_nothing_again
As you can see from the script above, this takes a bit of testing to get the iRules listed. Ofcourse, ideally either the API or SDK should return an empty list if there are no iRules on a Virtual Server, rather than a script-breaking "AttributeError: 'f5.bigip.tm.ltm.virtual.Virtual'>' object has no attribute 'rules'". SDK is a build up on top of a problematic API, so eventually this problem should be addressed by the API developers.
- jasvinder_singhAug 21, 2017Nimbostratus
python sdk
- Hannes_RappAug 21, 2017Nimbostratus
I see you already marked it as an accepted answer. Thx but normally you should do it after your question/problem has a working solution. If it just contributes to discussion and is useful, but not the actual solution, use the UP vote.
Anyway. Forgot to ask what is your BigIP version? I'll provide a working solution for 11.5.4 (most popular now) if you do not specify otherwise some time later today
- jasvinder_singhAug 21, 2017Nimbostratus
12.0.0.4
- Hannes_RappAug 21, 2017Nimbostratus
Edited my answer to give you the actual solution. Lmk if it works for you. If you're using Python 2 rather than Python 3, firstly, you should upgrade, but if you resist, modify the print statements to comply with your legacy standard :)
- Hannes_RappAug 22, 2017Nimbostratus
Regarding AttributeError, I opened an issue which can be tracked here: https://github.com/F5Networks/f5-common-python/issues/1170
Despite root problem being caused by API itself, SDK team is willing to implement a fix in their software in a future release. You can already merge the changes from a proposed fix (1195) to your SDK and this sample should already work without exception management.
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com