Forum Discussion

Christoph_Lange's avatar
Christoph_Lange
Icon for Altostratus rankAltostratus
Feb 22, 2017

tmsh script to change a profile in all virtual servers in all partitions

Inspired by some articles on devcentral ( in special Stop Using the Base TCP Profile! and Rapid iRule Removal via Tmsh Script ), I decided to write a tmsh script to replace a profile for all virtual servers in all partitions. Maybe someone can use it or has any hints for optimization.

Example:

root@(lab1)(cfg-sync Standalone)(Active)(/Common)(tmos) create cli alias shared changeprofile command "run cli script changeProfile.tcl"
root@(lab1)(cfg-sync Standalone)(Active)(/Common)(tmos) changeprofile /Common/tcp /Common/tcp-lan-optimized
The /Common/tcp profile was replaced with the /Common/tcp-lan-optimized profile for the following virtuals in partition /Common :
The /Common/tcp profile was replaced with the /Common/tcp-lan-optimized profile for the following virtuals in partition /LAB1 :
        testvip1-http
        testvip1-https
        testvip2-https

tmsh script:

cli script changeProfile.tcl {
proc script::init {} {
    set ::info "Usage: changeprofile     IMPORTANT: ALL virtual servers in ALL partitions!"
}

proc script::run {} {
    if { $tmsh::argc != 3 } {
        puts $::info
        exit 0
    }
    set old_profile [lindex $tmsh::argv 1]
    set new_profile [lindex $tmsh::argv 2]
    set all_partitions [tmsh::get_config auth partition]

    foreach partition $all_partitions {
        set current_partition "/[lindex [split $partition " "] 2]"
        tmsh::cd $current_partition
        set profiles ""
        set vips [tmsh::get_config /ltm virtual]

        puts "The $old_profile profile was replaced with the $new_profile profile for the following virtuals in partition $current_partition : "

        tmsh::begin_transaction

        foreach vip $vips {

            set profiles [tmsh::get_field_value $vip "profiles"]
            if { $profiles contains $old_profile } {
                tmsh::modify /ltm virtual [tmsh::get_name $vip] profiles delete "{" $old_profile "}" profiles add "{" $new_profile "}"
                puts "\t[tmsh::get_name $vip]"
            }
        }

        tmsh::commit_transaction

    }
}

proc script::help {} {
    if { $tmsh::argc != 3 } {
        tmsh::add_help $::info
    }
}

proc script::tabc {} {
    if { $tmsh::argc != 3 } {
        tmsh::add_tabc $::info
    }
}
}
No RepliesBe the first to reply