Forum Discussion

Tony_Hobson_189's avatar
Tony_Hobson_189
Icon for Nimbostratus rankNimbostratus
Jun 10, 2015

Variable Scope in iRules

Hello

I’ve got a quick question regarding variables in iRules. I would like to set variables to use across different functions in an iRule but struggling to find out what I need to do to make a variable available in the scope of the entire iRule. For example:

I want to set TEST once at the top of an iRule, but if I set this like shown below, I get [command is not valid in the current scope]

set TEST 1

when HTTP_REQUEST {
       if { $TEST } {
              do something
       }
}

When HTTP_RESPONSE {
       if { $TEST } {
              do something
       }
}

Thanks

  • Hi nitass

    Thanks for the reply. It looks like I can use static global variables instead:

    From https://devcentral.f5.com/articles/-the101-irules-101-variables

    Set a static variable value, which will exist until config reload, living outside of the scope of any one particular 
    set static::logserver "10.10.1.145"
    Reference that static variable just as you would any other variable from within any iRule
    log $static::logserver "This is a remote log message"
    

    The articles says: You can effectively set static global variable data in the static namespace without breaking CMP, and thereby not decreasing performance. To do so simply set up your static::varname variables, likely in RULE_INIT

    How do I invoke RULE_INIT and set variables in here?

    Thanks

    Tony