Forum Discussion
Mark_Stradling_
Oct 23, 2015Cirrus
Ok.... well I figured it out. I pulled an existing irule and saw the format. This is what it looks like when you request an iRule
[{'rule_name': '/Common/myrule.com', 'rule_definition': 'when CLIENT_ACCEPTED {\n set hsl [HSL::open -proto UDP -pool LOGGINGPOOL_514]\n} \n when HTTP_REQUEST {\n if {[HTTP::host] starts_with "somedomain"} {\n HSL::send $hsl "[clock format [clock seconds] -format "%m/%d/%Y %H:%M:%S %z"] F5-log-output httphost=[HTTP::host] URI=[HTTP::uri] refer=[HTTP::header "Referer"]" \n }\n}\n'}]
I managed to cobble together a script that works! It pulls all irules from one F5 and imports them into another after doing a string replace. We have the same rules in multiple data centers / dev env, and only one portion of the pool is different. This allows me to pull the rules from one F5 to another -- doing a transform before applying them. Here is my working code:
!/usr/bin/env python
import sys
import bigsuds
import ssl
import getpass
import time
'''SSL CERT FIX'''
'''------------'''
ssl._create_default_https_context = ssl._create_unverified_context
def get_pools(obj):
try:
return obj.LocalLB.Pool.get_list()
except Exception, e:
print e
def get_members(obj, pool):
try:
return pool, obj.LocalLB.Pool.get_member_v2(pool)
except Exception, e:
print e
def get_rules_list(obj):
try:
return obj.LocalLB.Rule.get_list()
except Exception, e:
print e
def get_rule(obj, rule):
try:
return obj.LocalLB.Rule.query_rule([rule])
except Exception, e:
print e
def create_rule(obj, rule_name, rule_definition):
try:
return obj.LocalLB.Rule.create([{'rule_name':rule_name,'rule_definition':rule_definition}])
except Exception, e:
print e
try:
b = bigsuds.BIGIP(
hostname = "source_load_balancer",
username = "admin",
password = "admin",
)
except Exception, e:
print e
try:
c = bigsuds.BIGIP(
hostname = "target_load_balancer",
username = "admin",
password = "admin",
)
except Exception, e:
print e
irules = get_rules_list(b)
for rule in irules:
rule_txt = get_rule(b, rule)
rule_name = rule_txt[0]["rule_name"]
rule_definition = rule_txt[0]["rule_definition"].replace("STRING_TO_REPLACE", "REPLACED_VALUE")
create_rule(c, rule_name, rule_definition)