Forum Discussion

bjtstarks_26108's avatar
bjtstarks_26108
Icon for Nimbostratus rankNimbostratus
Jan 15, 2019

How to use Lists in iRule

I'm trying to do a redirect to a specific node for a specific set of sites using our F5. I can do a single site easily using the following irule:

when HTTP_REQUEST {
    if { [string tolower [HTTP::host]] ends_with "site1.mysite.com" } {
        node 10.1.1.31
    }
}

And for a single site this is fine. My problem is I need to do this for about ~130 sites, and want to manage a list of sites that is called in the irule as these sites that are being redirected might be changed over time.

I created a list in my partition under System --> File management --> iFile List called MyList that was imported from MyList.txt on my local machine.

I then went to Local Traffic --> iRules --> Data Group List and created MyList as a string. I tried to add it as an address list, but got the following error:

The requested class IP item (/LB_Stage_Web/MyList 10.XXX.XXX.XXX%1 255.255.255.255) already exists in partition LB_Stage_Web.

So I'm just really wondering how I can use the list functionality to get this to work.

3 Replies

  • Check this out:

    https://devcentral.f5.com/articles/intermediate-irules-data-groups-20430

    You need to take the contents of the file you imported and set it to the key/values in the datagroup.

    ltm data-group internal my_datagroup {
    records {
        site1.mysite.com {
            data 10.1.1.31
        }
        site2.mysite.com {
            data 10.1.1.32
        }
        ...
        site130.mysite.com {
            data 10.1.1.130
        }
    }
    type string
    }
    

    Then you can do a single statement like:

    if { [class match [string tolower [HTTP::host]] equals "my_datagroup" ] } {
      node [class match -value [string tolower [HTTP::host]] equals "my_datagroup"
    }
    

    You could add the records in via the GUI or tmsh, or create the file like that and merge it in, or even add it to the bigip.conf and just to a tmsh load sys config.

    • bjtstarks_26108's avatar
      bjtstarks_26108
      Icon for Nimbostratus rankNimbostratus

      This looks like what I was looking for. Thanks! I'll give it a try and let you know how it goes.

       

  • Better solution is to configure local traffic policies!

     

    With local traffic policy, create a new policy with one rule

     

    • name redirect
    • condition :
      • http- host host is in
        • site1.mysite.com
        • site2.mysite.com
        • site3.mysite.com
    • action :
      • forward node 1.2.3.4