Remove iRule From Multiple Virtual Servers

Problem this snippet solves:

Remove an iRule from one or more virtual servers. This script will search through the BIG-IP virtual servers for the specified iRule and remove it from any virtual server (not from the system).

How to use this snippet:

Script output

# run cli script delrule.tcl filler2
The filler2 iRule was removed from the following virtuals:
  tmshtest2
 tmshtest3
 tmshtest4
 tmshtest5
 tmshtest7
 tmshtest8
 tmshtest9
 tmshtest10 
 

Code :

script delrule.tcl {

proc script::run {} {
    if { $tmsh::argc != 2 } {
       puts "A single rule name must be provided"
       exit
   }
   set rulename [lindex $tmsh::argv 1]
   set rules ""
   set vips [tmsh::get_config /ltm virtual]
   set vips_in_play "" 

    tmsh::begin_transaction 

    foreach vip $vips { 
# Rule keyword not present
        if { [tmsh::get_field_value $vip "rules" rules] == 0 } {
           continue
        }
# Rule keyword is present, but not $rulename rule
        if { [lsearch -exact $rules $rulename] == -1 } {
           continue
        }
# If only 1 rule applied, can use the none attribute, otherwise, must
# "pop" $rulename from the rule list and re-apply
        if { [llength $rules] < 2 } {
           tmsh::modify /ltm virtual [tmsh::get_name $vip] rules none
           lappend vips_in_play $vip
       } else {
           set id [lsearch -exact $rules $rulename]
           set keepers [lreplace $rules $id $id]
           tmsh::modify /ltm virtual [tmsh::get_name $vip] rules "{ $keepers }"
           lappend vips_in_play $vip
       }
    }

    tmsh::commit_transaction 

    puts "The $rulename iRule was removed from the following virtuals: "
   foreach x $vips_in_play {
       puts "\t[tmsh::get_name $x]"
    }
}


}
Published Mar 10, 2015
Version 1.0
  • Code Fork /w Multiple Partition Support: https://devcentral.f5.com/s/articles/remove-irule-from-multiple-virtual-servers-fork-w-multiple-partition-support