Forum Discussion
iControlREST replace iRule references on all virtual servers
Since the task requires search and manipulation of the returned JSON data, you should consider writing a script rather than combining a number of $1 calls. A Python example code below should do the job.
from sys import argv
from f5.bigip import ManagementRoot
import urllib3
Disable SNIMissingWarning and InsecurePlatformWarning
urllib3.disable_warnings()
nameOldRule = argv[1]
nameNewRule = argv[2]
mgmt = ManagementRoot('ltm1311', 'admin', 'admin') Change me
Get me all the virtuals
virtualCollection = mgmt.tm.ltm.virtuals.get_collection()
for v in virtualCollection: All the virtuals
vsname = v.__dict__['name'] Get the virtual name
try:
oldRules = v.__dict__['rules'] Check if it has any rules
except KeyError:
continue
try: Check if it has the nameOldRule
index = oldRules.index(nameOldRule)
except ValueError:
continue
newRules = oldRules
newRules[index] = nameNewRule Replace nameOldRule > nameNewRule
vs = mgmt.tm.ltm.virtuals.virtual.load(name=vsname)
vs.update(rules=newRules) Update the virtual
print('Updated {}: {} > {}'.format(vsname, oldRules, newRules))
Specify the old and new rules in full path as the 1st and 2nd arguments to the program: e.g., $1. It first gets all the virtuals on the box ($1). Then, for each virtual, check if it has any rules (otherwise go to next virtual), check if has the old rule, then replace that element with the new one and put it back to the virtual ($1). The rules attached to a virtual is stored in a list (array), so they are ordered: The order of rules won't change.
See also F5 Python SDK Documentation.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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
