Forum Discussion

Mark_Stradling_'s avatar
Oct 23, 2015

Python + bigsuds: What format to create iRule

I am not able to find an example of this anywhere and I am having a lot of trouble creating an iRule with bigsuds + python. I cannot figure out how to deliver the data to the Rule.create method. I am trying to pull the iRules from one F5 and import them into another. I get various errors when I try to pass the rule_name and rule_definition to the create rule method. I've tried all kinds of brackets and quotes and spaces... Any help would be appreciated. I've

!/usr/bin/env python
import sys
import bigsuds
import ssl
import getpass

'''SSL CERT FIX'''
'''------------'''

ssl._create_default_https_context = ssl._create_unverified_context

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):
    try:
        return obj.LocalLB.Rule.create(obj,rule, rule_txt)
    except Exception, e:
        print e

try:
    b = bigsuds.BIGIP(
            hostname = "192.168.1.2",
            username = "admin",
            password = "admin",
            )
except Exception, e:
    print e

try: c = bigsuds.BIGIP( hostname = "192.168.1.1", username = "admin", password = "admin", ) except Exception, e: print e

irules = get_rules_list(b) print irules for rule in irules: rule_txt = get_rule(b, rule) create_rule(c, rule, rule_txt)

2 Replies

  • Gah.... Stupid thing won't let me fix formatting !/usr/bin/env python import sys import bigsuds import ssl import getpass '''SSL CERT FIX''' '''------------''' ssl._create_default_https_context = ssl._create_unverified_context 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): try: return obj.LocalLB.Rule.create(obj,rule, rule_txt) except Exception, e: print e try: b = bigsuds.BIGIP( hostname = "192.168.1.2", username = "admin", password = "admin", ) except Exception, e: print e try: c = bigsuds.BIGIP( hostname = "192.168.1.1", username = "admin", password = "admin", ) except Exception, e: print e irules = get_rules_list(b) print irules for rule in irules: rule_txt = get_rule(b, rule) create_rule(c, rule, rule_txt)
  • 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)