Forum Discussion
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
- nitassEmployee
variable has to be set inside event (i.e. when).
The101: iRules 101 - Variables by Colin Walker
- mikeshimkus_111Historic F5 Account
You can use static to set it when the rule initializes: https://devcentral.f5.com/wiki/iRules.static.ashx
when RULE_INIT { set static::TEST 0 } when HTTP_REQUEST { if { $static::TEST } { do something } } When HTTP_RESPONSE { if { $static::TEST } { do something } }
- Tony_Hobson_189Nimbostratus
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
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com