Initialize Rule

Problem this snippet solves:

This iControl sample will force the re-initialization of an iRule.

There are several instances where the internal configuration of BIG-IP caches contents of it's configuration objects. iRules are an example of this. Upon first load of the configruation, all rules are pre-compiled into bytecode and stored in the runtime engine. At the point of load, the configuration engine looks at the checksum of the iRule to determine whether it already exists. If the checksum already exists, the load is ignored.

So if you find yourself in the case where you want to force a initialize on a rule when it's already loaded into the configuration, a simple save of the iRule won't do it. If the checksum is the same, a rule init will not occur. You'll heed to modify the content to allow for a different checksum.

This iControl app, downloads the content of an iRule, adds a space at the end, saves that modified iRule, and then re-saves the original non-space iRule returning the configuration back to it's original state.

An example of when you would need to do this would be when an iRule accesses an external data group and you want to ensure that the data group is reloaded into the configuration.

Code :

# The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5 
# Software Development Kit for iControl"; you may not use this file except in
# compliance with the License. The License is included in the iControl
# Software Development Kit. 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# The Original Code is iControl Code and related documentation
# distributed by F5.
# The Initial Developer of the Original Code is F5 Networks,
# Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2006 F5 Networks,
# Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above.  If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.

#use SOAP::Lite + trace => qw(method debug);
use SOAP::Lite;

# Variables
my $DEBUG = 0;
my $sHost = $ARGV[0];
my $sPort = $ARGV[1];
my $sUID = $ARGV[2];
my $sPWD = $ARGV[3];
my $sRuleName = $ARGV[4];
my $sProtocol = "http";

if ( $sPort ne "80" )
        $sProtocol = "https";

# Transport Information
sub SOAP::Transport::HTTP::Client::get_basic_credentials
        return "$sUID" => "$sPWD";

# iControl interface declarations
$LocalLBRule = SOAP::Lite
        -> uri('urn:iControl:LocalLB/Rule')
        -> readable(1)
        -> proxy("$sProtocol://$sHost:$sPort/iControl/iControlPortal.cgi");

# Main logic

# sub invalidateRule
# This iRule will query the contents of an iRule, save it with an added space
# and then re-save it with the original contents.
sub invalidateRule()
        if ( $DEBUG ) { print "Querying rule definition for rule '$sRuleName'\n"; }

        # Query the rule definition
        $soapResponse = $LocalLBRule->query_rule(SOAP::Data->name(rule_names => [$sRuleName]));
        @RuleDefinitionList = @{$soapResponse->result};

        # Extract the first element in the array
        $RuleDefinition = @RuleDefinitionList[0];
        # save the original and temporary changed values
        $rule_definition = $RuleDefinition->{"rule_definition"};

undef $/;
$_= $rule_definition;

# print $_;

if (/\n\n$/) {
# print "does have new line: $_\n";
chop $rule_definition;
$new_rule_definition= $rule_definition;
} else {
# print "does not have new line: $_\n";
$new_rule_definition = "$rule_definition\n";

        # update RuleDefinition with the new one
        if ( $DEBUG ) { print "modifying with temporary changes...\n"; }
        $RuleDefinition->{"rule_definition"} = $new_rule_definition;
        $soapResponse = $LocalLBRule->modify_rule(SOAP::Data->name(rules => [$RuleDefinition]));

        if ( $DEBUG ) { print "rule initialized...\n"; }

# sub checkResponse
# Exit on SOAP Fault.
sub checkResponse()
        my ($soapResponse) = (@_);
        if ( $soapResponse->fault )
                print $soapResponse->faultcode, " ", $soapResponse->faultstring, "\n";
Published Mar 07, 2015
Version 1.0

Was this article helpful?

1 Comment

  • Including the line below in the beginning can avoid the warning message "certificate verify failed", $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; Thanks for the script! It works!