Sie sind auf Seite 1von 3

iRules 101 - #03 - Variables

Colin Walker, 2007-19-11

As you're beginning to get more comfortable reading and writing iRules, you're going to continue expanding your
toolbox as well as the tasks you're going to tackle. If your experience is anything like mine then sooner, rather than later,
you'll be looking at ways to read, store, and manipulate different types of content. It's at this point in your "iRuling" career
that variables begin coming into play more regularly and an understanding of what they are and how they're handled in
iRules would probably be useful. Other articles in the series:
iRules 101 #01 Introduction to iRules
iRules 101 #02 If and Expressions
iRules 101 #03 Variables
iRules 101 #04 Switch
iRules 101 #05 Selecting Pools, Pool Members, and Nodes
iRules 101 #06 When
iRules 101 #07 Catch
iRules 101 #08 Classes
iRules 101 #09 Debugging
iRules 101 #10 Regular Expressions
iRules 101 #11 Events
iRules 101 #12 The Session Command
iRules 101 #13a Nested Conditionals
iRules 101 #13b TCL String Commands Part 1
iRules 101 #14 TCL String Commands Part 2
iRules 101 #15 TCL List Handling Commands
iRules 101 #16 Parsing String with the TCL Scan Command
iRules 101 #17 Mapping Protocol Fields with the TCL Binary Scan Command
Setting, Unsetting and Manipulating Variables
iRules makes use of standard TCL variables, for the most part, and as such most of the commands you'd use to set,
unset, evaluate and effect variables in TCL still apply. Many of these commands are the same regardless of what type of
data structure (variable) you're creating, and are straight-forward in their naming and behavior. For example, to set (or
create) a variable, you simply use the command set:
setmsg"Hello,World!"

The same command applies even when setting more complex structures and lling them with different types of data,
static or dynamic alike. A few more examples could look like:
setr[exprrand()]
setvblin[expr{rand()>=0.5}]
setreqArray([IP::client_addr])[getfield[stringtolower[HTTP::uri]]"?"2]

Now that you know how to set a variable, the next thing you should probably learn is how to delete it once you're done
with it, even though iRules is pretty good about taking out the trash, but more on that later. To delete a variable that
you've already created, you simply use the unset command, which looks like:
unsetmsg

This particular command can take multiple arguments, as well. To delete all the basic variables we set above, for example,
you could simply use:

This particular command can take multiple arguments, as well. To delete all the basic variables we set above, for example,
you could simply use:
unsetmsgrvbl

Now that you can set and delete variables, what other kinds of fun things can you do to them? I mean, we all know
they're meant to be used in place of static data in commands like pool and log, but what other commands do you have
available to effect variables that are already in memory? The real answer is "lots!", but here are a few fun ones.
append
append is used for appending data to an already existing variable (Note: will also create the variable if it doesn't exist):
setvar0
for{seti1}{$i<=10}{incri}{
appendvar","$i
}
puts$var
#Prints0,1,2,3,4,5,6,7,8,9,10

concat
The concat command will join all values provided as arguments together into a list, seperated by white space:
concat"ab{c"d"e}f"
willreturn"ab{cde}f"asitsresult.

incr
By using the incr command you can increment the value of a variable either by 1 if no value is supplied, or by a desired
value:
Addonetothecontentsofthevariablex:
incrx
Add42tothecontentsofthevariablex:
incrx42

Variable Scopes (local vs. global)


One of the largest differences between standard TCL and iRules, as far as variable contruction and interaction are
concerned, is that in iRules there are effectively only two namespaces - local and global. All "normal" variables are under
the local scope. This means that they are connection based. You set the variable in the iRule, it remains present for the
length of that connection, and is torn down when the connection is reaped. This is typically just a Request/Response
pair but could span multiple Requests/Responses depending on the protocol, settings, etc. These variables are the most
efcient, safest, and are what we use and recommend 98% of the time.
So what, you ask, makes up that other 2%? Global variables. A global variable in an iRule is set much the same way, can
be inuenced with the same commands just like the "normal" (local) variables in the demonstrations above, with one key
difference; global variables are not tied to a session, but rather to a TMM instance. This means that you can access the
data contained inside a gobal variable not just across events, but across connections as well. This can range from
unnecessary to absolutely required depending on the task you're trying to accomplish (such as a counter that needs to
extend across connections for something like a rate-limiting lter).
We tend to only use global variables when they are absolutely necessary for a couple of reasons. Efciency, rst, and
ease of use, second. Since these variables aren't torn down nicely by TMM after every connection, you suddenly have to
worry about variable scope and management, rather than just setting and forgetting. This isn't a horrendous

We tend to only use global variables when they are absolutely necessary for a couple of reasons. Efciency, rst, and
ease of use, second. Since these variables aren't torn down nicely by TMM after every connection, you suddenly have to
worry about variable scope and management, rather than just setting and forgetting. This isn't a horrendous
undertaking, but something to keep in mind whenever you may end up using a global variable. If you set it, you'd better
have a plan for unsetting it, too. This is especially true if you have multiple VIPs and or rules accessing a single variable as
whatever value is last set will remain in place until the variable is updated or unset, which can cause unexpected behavior
and even errors in some situations.
Lastly, how do you know which variables are which? Luckily this is easy. Global variables are dened and signied by a
double colon before the variable name, such as ::myGlobal. You can spot these easily in an iRule because any variable
starting with a double colon in this fashion is treated as a global variable, so there shouldnt' be any confusion when you
see set ::r [expr rand()] instead of set r [expr rand()]. The only time this becomes even slightly confusing is when
setting variables in the RULE_INIT event. All variables set under this event are treated as global, so keep that in mind.
The same commands may be used against either local or global variables, though, so keep an eye out.
For more information on the commands I talked about see:
set
unset
append
concat
incr
And for a list of available TCL commands to see what other fun things you can do to/with variables, check out:
TCL Commands list
Get the Flash Player to see this player.

F5 Networks, Inc. | 401 Elliot Avenue West, Seattle, WA 98119 | 888-882-4447 | f5.com
F5 Networks, Inc.
Corporate Headquarters
info@f5.com

F5 Networks
Asia-Pacific
apacinfo@f5.com

F5 Networks Ltd.
Europe/Middle-East/Africa
emeainfo@f5.com

F5 Networks
Japan K.K.
f5j-info@f5.com

2016 F5 Networks, Inc. All rights reserved. F5, F5 Networks, and the F5 logo are trademarks of F5 Networks, Inc. in the U.S. and in certain other countries. Other F5
trademarks are identified at f5.com. Any other products, services, or company names referenced herein may be trademarks of their respective owners with no
endorsement or affiliation, express or implied, claimed by F5. CS04-00015 0113

Das könnte Ihnen auch gefallen