Forum Discussion

panos_101277's avatar
panos_101277
Icon for Nimbostratus rankNimbostratus
Apr 29, 2008

Multiple irules using same local variable name

Hello,

 

 

First off thanks to all the contributors for their many examples and tips, it has really helped a lot.

 

 

My question is a simple one but I haven't been able to find an answer or figure out a way around it.

 

 

Is there a way for a variable to be specific to the irule it is created in? Ie can I do this:

 

 

irule1:

 

 

when HTTP_REQUEST {

 

set myvar "some value"

 

}

 

 

 

irule2:

 

when HTTP REQUEST {

 

set myvar "another value"

 

}

 

 

 

Note that these two rules would be attached to the same virtual server. The reason I'm asking is because I've been trying to setup my rules so that they share a lot of common code with local variables to point to different classes to define their behavior. However when I try to use them the local variables in the different irules all have the same value which suffice to say is less than ideal [Tongue]

 

 

 

(I originally posted this under iControl by mistake, since I can't move it I've reposted it here)

5 Replies

  • Hi,

     

     

    Global variables (like $::my_var) are shared across all iRules on all VIPs on all TCP connections. Local variables (like $my_var) are shared across the same TCP connection for any iRule(s) associated with the same virtual server. So if you set myvar in rule1 on vip1 and add a second rule to vip1, that same variable instance is accessible (and can be trampled) in rule2. If you want distinct local variable instances on multiple rules on the same VIP, you can name them uniquely. Using unique variable names per VIP is also important for global variables if you don't want other rules to be able to impact a separate rule. A good example of this is using a global debug variable. You probably wouldn't want debug enabled in every rule if you change the global variable in a single rule. So you can use a naming convention that includes the rule name or application (like ::irule1_debug instead of ::debug).

     

     

    Aaron
  • That's what I was afraid of..

     

     

    You see the problem with that approach is that I end up using the same code with slightly different variables and when I make a change in one rule I'll need to make the same change in all the other rules which are basically the same except the variables.

     

     

    It seems to me that there really should be a way to make variables local per irule.
  • It is somewhat inconvenient. You could try something like setting a single local variable (not in RULE_INIT) in each rule to something unique and then build the variable names in that particular rule based on the first variable. Maybe an example would be easier to demonstrate:

    
    rule rule1 {
       when CLIENT_ACCEPTED {
          set var_prefix "rule1"
          set ${var_prefix}var1 "this is rule1's var1 value"
          log local0. "$var_prefix variable name ${var_prefix}var1, variable value: [set [subst ${var_prefix}var1]]"
       }
    }

    
    rule rule2 {
       when CLIENT_ACCEPTED {
          set var_prefix "rule2"
          set ${var_prefix}var1 "this is rule2's var1 value"
          log local0. "$var_prefix variable name ${var_prefix}var1, variable value: [set [subst ${var_prefix}var1]]"
       }
    }

    Here is the sample output from a request to a vip with both rule1 and rule2 added:

    
    Rule test_rule1 : rule1 variable name rule1var1, variable value: this is rule1's var1 value
    Rule test_rule2 : rule2 variable name rule2var1, variable value: this is rule2's var1 value

    I guess you could put in a feature request with F5 support asking for the scope of local variables to be limited to the rule they're set in, but I'm guessing there is a good reason for the way it is now.

    Aaron
  • What you're saying makes sense. I opened a case a bit ago asking F5 to support user-defined functions. If you want to push for this as well, you can open a case with F5 support (websupport.f5.com) and ask them to attach your case to the existing request. Check here for details: (Click here).

     

     

    You might also ask support to look into your suggestion on isolating the scope of local variables to just the rule they're defined in.

     

     

    Aaron
  • Yeah I'll do that definitely. Well to allow user-defined functions they would also need to have variable scope per function, that is better than having variable scope per irule.