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.
@jaykumar_f5 i tried this. It seems to give input but not the exact output like
vip1 - irule1 irule2
vip2 irule1 irule2
my exact requirements is to know how many vip's have one particular vip attached {code}
!/usr/bin/python
from import BigIP import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
Connect to the BigIP
bigip = BigIP("hostname", "username", "password")
Get a list of all pools on the BigIP and print their name and their members' name
for virtual in bigip.ltm.virtuals.get_collection(): virt = bigip.ltm.virtuals.virtual.load(partition=virtual.partition, name=virtual.name) rule=virt.rules for rule in rules: print "Partition: %s Virtual: %s rule: %s" % (virtual.partition, virtual.name, rule.name) {code}
Hi
You mentioned "my exact requirements is to know how many vip's have one particular vip attached {code}"
Do you mean you need to know how many vip's are using a particular iRule? If so you can add a for loop with the code provided earlier to accomplish this
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()
IRULE_COLLECTION = API_ROOT.tm.ltm.rules.get_collection()
for rule in IRULE_COLLECTION:
count = 0
print('iRule Evaluated: ', rule.name)
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 vs_rule in vserver.rules:
if vs_rule == rule.fullPath:
print(' ', virtual.name)
count += 1
print('iRule: ', rule.name)
print('Used: ', count)
Output should look similar to this
iRule Evaluated: _sys_https_redirect
app1_vs virtual server using the irule
app2_vs virtual server using the irule
iRule: _sys_https_redirect iRule with count
Used: 2
Hope this helps
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