# Passing a variable through an iRule

**URL:** <https://community.f5.com/t/passing-a-variable-through-an-irule/9382>\
**Category:** Technical Forums\
**Tags:** design, availability, irules, management, devops, application-delivery\
**Created:** [October 14, 2013, 6:37pm UTC](https://community.f5.com/t/passing-a-variable-through-an-irule/9382 "2013-10-14T18:37:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robberhines\_120](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@Robberhines\_120](https://community.f5.com/u/Robberhines_120)\
**Post date:** [October 14, 2013, 6:37pm UTC](https://community.f5.com/t/passing-a-variable-through-an-irule/9382/1 "2013-10-14T18:37:42Z")

</div>

We have the following maintenance test page, it’s based off the standard maintenance page. What we are trying to do is create a dynamic maintenance page for each site(pool) so all we would have to do is just upload two ifiles and the irule wouldn’t have to be adjusted. so lets say someones tries to go to [www.abc.com](http://www.abc.com), hits the F5, goes to the [www.abc.com](http://www.abc.com) pool, however all of it’s memebers are down. we have iFiles called [www.abc.com.html](http://www.abc.com.html) and [www.abc.com.jpg](http://www.abc.com.jpg), the irule would find the files for [www.abc.com](http://www.abc.com) and then display their own maintenance page. Later on we would add files for [www.xyz.com](http://www.xyz.com) and if [www.xyz.com](http://www.xyz.com) is down, their custom page would be displayed.

```tcl
when CLIENT_ACCEPTED {
                    set default_pool [LB::server pool]
    }
    when HTTP_REQUEST { 
        set htmlfile
        set imagefile
        if { [active_members $default_pool] < 1 }{
            $htmlfile = lsearch -exact $default_pool.html
            $imagefile = lsearch -exact $default_pool.jpg
            log local0. $imagefile
                if{!($htmlfile equals $default_pool)} {
                    if { [HTTP::uri] contains $imagefile } { 
                    HTTP::respond 200 content [ifile get $imagefile] noserver "Cache-Control" no-cache "Server" 
                    } else {                
                    log local0. "[HTTP::host] for client [client_addr]"
                    HTTP::respond 200 content [ifile get $htmlfile] noserver "Cache-Control" no-cache "Server" 
                    }

            }
        }
    }

```

with the above I get the following message when I tail the LTM log

_err tmm[8134]: 01220001:3: TCL error: /Common/Maint\_test - can’t read “htmlfile”: no such variable while executing “set htmlfile”_

---

<div class="post-metadata">

**Author:** ![Hamish](https://avatars.discourse-cdn.com/v4/letter/h/a5b964/32.png) [@Hamish](https://community.f5.com/u/Hamish)\
**Post date:** [October 14, 2013, 6:58pm UTC](https://community.f5.com/t/passing-a-variable-through-an-irule/9382/2 "2013-10-14T18:58:12Z")

</div>

Ah… You’re a C programmer? Java perhaps? (I sympathise. I prefer C or perl myself. Hate Java). But I digress… This is TCL… The syntax for setting your variables is incorrect. You SET a variable to assign it a value. Not to declare it. So the

```tcl
set htmlfile

```

doesn’t need to be there (Unless you want toset it to some default value like

```tcl
set htmlfile ""

```

Which gets rid of your first (And only reported so far) error. But the syntax where you set it to explicit values is also wrong for TCL. It should be something like

```tcl
set htmlfile [lsearch -exact $default_pool.html]

```

But I’m unsure what you’re trying to do there. leasrch searches a list… But $default\_pool.html isn’t a list… Reading the rest of the logic perhaps you just meant something like

set htmlfile “$default\_pool.html”

Perhaps? As $htmlfile is supposed to be the name of a file to return the contents of?

H

---

<div class="post-metadata">

**Author:** ![Robberhines\_120](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@Robberhines\_120](https://community.f5.com/u/Robberhines_120)\
**Post date:** [October 14, 2013, 7:33pm UTC](https://community.f5.com/t/passing-a-variable-through-an-irule/9382/3 "2013-10-14T19:33:58Z")

</div>

Hamish,

the reason we are doing the lsearch is because the pool name may not always be [www.abc.com](http://www.abc.com), it might just be in as [contoso.com](http://contoso.com) with no www. So the lsearch is in there to locate the proper html and jpg in the LTM’s ifiles location for the pool/site that is trying to be called. in our case we may have many different files for the number of domains we host.

so you are correct in assuming what we are trying to do with $htmlfile (and $imagefile) is to get the corresponding file back from the list of ifiles we would have. again say the client accepted pool is just call [abc.com](http://abc.com) but the file search comes back with www.abc.com.html, we would then want to display that html and jpg file
