Forum Discussion

Mick39_201768's avatar
Mick39_201768
Icon for Nimbostratus rankNimbostratus
May 22, 2015

How to add iRules using CLI

Hi, team,

 

I'm looking for the command to add iRule, since I have to apply about 40 same iRule to Virtual servers. I tried command which I learned from Q&A below, but it delete current iRules.

 

tmsh modify ltm virtual ABC.COM-HTTP rules { Maintenance-Irule }

 

ABC.COM-HTTP = Virtual Server, Maintenance-Irule = iRule which I want to add

 

(cf:) https://devcentral.f5.com/questions/how-to-apply-irule-through-cli-idea-is-to-share-the-tmsh-script-to-create-a-batch-script-which-windows-team-can-work-edit-accordingly

 

Could you tell me how can I just ADD the iRule? Thanks in advance for your help, Naoki

 

5 Replies

  • I've done this same task with this script I've made. I add an iRule preserving all existing ones on the Virtual Server.

     

    #!/bin/sh

     

    # f5-irule-add - add iRule to a virtual server, preserving existing iRule configuration

     

    # Gian Henriques, 30/06/2021

     

    # hot to use: ./script.sh new_irule my_vs

    # source: https://devcentral.f5.com/s/question/0D51T00006i7Y7R/add-irule-via-tmsh-without-deleting-existing-irules-and-how-to-reorder

     

    virtual=$2

    irule_add=$1

     

     #Capture existing iRule configuration

    tmsh list ltm virtual ${virtual} one-line | grep -q " rules { "

    if [ $? -eq 1 ]; then

      echo "$0: ${virtual} currently contains no rules; adding new rule"

      irule_current=""

    else

      irule_current=`tmsh list ltm virtual ${virtual} one-line | sed -e 's/.* rules { //' -e 's/ }.*//'`

     

     #  Check if rule already exists

      exists=0

      for rule in ${irule_current}

      do

       if [ ${rule} == ${irule_add} ]; then

         exists=1

       fi

      done

      if [ ${exists} -eq 1 ]; then

       echo "$0: ${irule_add} already exists in virtual ${virtual}" >&2

       #exit 2

      else

      # Modify iRule list

    command="tmsh modify ltm virtual ${virtual} rules { ${irule_current} ${irule_add} }"

    echo ${command}

    exec ${command}

    status=$?

    if [ ${status} -ne 0 ]; then

    echo "tmsh returned error status ${status}" >&2

    exit ${status}

    fi

    fi

    fi

     

  • Could you tell me how can I just ADD the iRule?

     

    i am afraid you have to specify all the irules.

     

  • You could still automate this but for each virtual you would have to run a list command and then parse the output.

     for all in `tmsh list ltm virtual VIRTUAL_NAME rules | egrep -v "\{|\}"`
    > do
    > echo $all
    > done
    firepass_assign
    
    

    This is a sample that will allow you to loop through the current rules assigned.

    When you have the current values then append the new iRule on the list and then run your modify command with all the iRules specified.

    Seth

  • You can automate the irule addition to the Virtual server with the below scripts. Create a file virtual_list and paste all the virtual servers for which you need to add the new irule.

    !bin/bash
    for i in $(cat virtual_list)
    do
    rule=`tmsh list ltm virtual $i rules | egrep -v "\{|\}"`
    echo "irule is ${rule//[[:blank:]]/} and virtual is $i"
    modify ltm virtual $i rules { ${rule//[[:blank:]]/} new-irule-name } 
    echo " rule added successfully "
    done
    
  • You can automate the irule addition to the Virtual server with the below scripts. Create a file virtual_list and paste all the virtual servers for which you need to add the new irule.

    !bin/bash
    for i in $(cat virtual_list)
    do
    rule=`tmsh list ltm virtual $i rules | egrep -v "\{|\}"`
    echo "irule is ${rule//[[:blank:]]/} and virtual is $i"
    modify ltm virtual $i rules { ${rule//[[:blank:]]/} new-irule-name } 
    echo " rule added successfully "
    done