Forum Discussion

wilcdr's avatar
wilcdr
Icon for Nimbostratus rankNimbostratus
Nov 02, 2017

Dynamically creating static variables per virtual server

Not sure if this is something that can even be done but I can't get it to work.

I'm trying to create an iRule that I can generically use to attach to an undefined number of VIPs and I need to have a "static" variable that is scoped to the virtual server that I'm attached to but not globally. Then have another VIP that will be used to control the variable.

I tried to do something like the following but it doesn't work.

On the controlling VIP (the one I will use to change the variable), let's call it VS-variable-change

 call it like 
    curl http://:/changeVariable?VIPName=VS-variable-test
when HTTP_REQUEST {
    if { [HTTP::uri] starts_with "/changeVariable" } {
        set VIPName [string tolower [URI::query [HTTP::uri] VIPName]]
        set varName "static::/Common/${VIPName}_myVar"
        log local0. "varName = $varName"
        set [subst $varName] 1
        log local0. "value = [subst $varName]"
    }
}

Then on VS-variable-test I attach the following iRule to try to read this value

when HTTP_REQUEST {
    if { [HTTP::uri] starts_with "/checkMyVariable" } {
        set varName "static::[virtual name]_myVar"
        log local0. "varname = $varName"
        log local0. "value = [subst $varName]"
    }
}

When I call VS-variable-change to change the variable, I get the following output

Rule /Common/irule-var-change : varName = static::/Common/vs-variable-test_myVar
Rule /Common/irule-var-change : value = static::/Common/vs-variable-test_myVar

So I thought it was because I didn't dereference the subst properly, so I changed from

[subst $varName]
to
[subst $$varName]

Now I get

Rule /Common/irule-var-change : varName = static::/Common/vs-variable-test_myVar
TCL error: /Common/irule-var-change  - can't read "static::": no such variable while executing "subst $$varName"

Is there a way to do this? I'm probably just confused about how to dereference that variable name.

Thanks!

5 Replies

  • Why don't you use the table command instead. You can use the virtual name

    [virtual name]
    in the name of the table_entry

     

    You can even set a lifetime

     

    Have a look at:

    https://clouddocs.f5.com/api/irules/table.html

     

  • wilcdr's avatar
    wilcdr
    Icon for Nimbostratus rankNimbostratus

    The table thing works. I guess I'll go this path. Was trying to see if I can get to work with a variable thinking the lookup would be faster than the table, but couldn't figure out how to get it to work.

     

    Thanks!

     

  • wilcdr's avatar
    wilcdr
    Icon for Nimbostratus rankNimbostratus

    I guess using the table command is the way to go, but if anyone can tell me why my variable thing originally didn't work (and how to make it work), it would be good for my academic interest in the topic. :)

     

    Thanks!

     

  • look at this article about Some iRule commands temporarily suspend iRule processing

    if you change the value every multiple times an hour, table may be a good solution. if this value is changed less than once an hour, the static value is still the best solution.

    you can use array to store this variable

    when HTTP_REQUEST {
        if { [HTTP::uri] starts_with "/changeVariable" } {
            set VIPName [string tolower [URI::query [HTTP::uri] VIPName]]
            set static::myVar(/Common/${VIPName}) foo
            log local0. "Variable name = static::myVar(/Common/${VIPName})"
            log local0. "value = $static::myVar(/Common/${VIPName})"
        }
    }