# Adding a iRule to a Virtual Server

**URL:** <https://community.f5.com/t/adding-a-irule-to-a-virtual-server/35340>\
**Category:** Technical Forums\
**Tags:** icontrol, python-icontrol-library, devops\
**Created:** [November 19, 2010, 6:13pm UTC](https://community.f5.com/t/adding-a-irule-to-a-virtual-server/35340 "2010-11-19T18:13:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jomar\_46001](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@Jomar\_46001](https://community.f5.com/u/Jomar_46001)\
**Post date:** [November 19, 2010, 6:13pm UTC](https://community.f5.com/t/adding-a-irule-to-a-virtual-server/35340/1 "2010-11-19T18:13:35Z")

</div>

Hi,

I was trying to write a script to allow me to add an existing iRule to an existing Virtual Server. I am receiving the following error when I run the script: ‘Could not find element by name: rule\_name’. Any help would be appreciated.

Below is the script:

!/usr/bin/env python

import sys

import pycontrol.pycontrol as pc

b = pc.BIGIP(

hostname = “F5l”,

username = “username”,

password = “Password”,

fromurl = True,

wsdls = [‘LocalLB.VirtualServer’])

c = b.LocalLB.VirtualServer

def convert\_rule(z,rule):

rule\_seq = z.LocalLB.VirtualServer.typefactory.create(‘LocalLB.VirtualServer.VirtualServerRuleSequence’)

rule\_set = z.LocalLB.VirtualServer.typefactory.create(‘LocalLB.VirtualServer.VirtualServerRule’)

rule\_set.rule\_name = rule

rule\_set.priority = 500

rule\_seq = [rule\_set]

return rule\_seq

c.add\_rule([‘VIP’],convert\_rule(b,‘iRule’))

---

<div class="post-metadata">

**Author:** ![L4L7\_53191](https://avatars.discourse-cdn.com/v4/letter/l/f475e1/32.png) [@L4L7\_53191](https://community.f5.com/u/L4L7_53191)\
**Post date:** [December 5, 2010, 4:16am UTC](https://community.f5.com/t/adding-a-irule-to-a-virtual-server/35340/2 "2010-12-05T04:16:42Z")

</div>

The add\_rule() method expects to have a keyword argument called ‘rules’ - Suds does a decent job of figuring out unambiguous keyword arguments, but for anything with embedded objects it’ll have problems.

[http://devcentral.f5.com/wiki/default.aspx/iControl/LocalLB\_\_VirtualServer\_\_add\_rule.html](http://devcentral.f5.com/wiki/default.aspx/iControl/LocalLB __VirtualServer__ add_rule.html)

So try something like:

```tcl
c.add_rule(['VIP'], rules = convert_rule(b, 'iRule'))

```

Hth,

-Matt

---

<div class="post-metadata">

**Author:** ![MYLK](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@MYLK](https://community.f5.com/u/MYLK)\
**Post date:** [March 27, 2012, 1:38pm UTC](https://community.f5.com/t/adding-a-irule-to-a-virtual-server/35340/3 "2012-03-27T13:38:14Z")

</div>

Hello,

I tried to use this code on my 10.2.3 LTM without success. I get either a similar error as the above user or no error at all and the irule is not applied.

My goal is to remove one irule and add another to many Virtual Servers at once.

Is there an example out there?

Thanks,

Matt

---

<div class="post-metadata">

**Author:** ![Thomas\_Weisshaa](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@Thomas\_Weisshaa](https://community.f5.com/u/Thomas_Weisshaa)\
**Post date:** [September 30, 2012, 8:09pm UTC](https://community.f5.com/t/adding-a-irule-to-a-virtual-server/35340/4 "2012-09-30T20:09:20Z")

</div>

Hi Jomar,

unfortunately I couldn’t figure it out as well with pyControl2 yet.

Here is a solution based on SOAPpy:

```tcl
!/usr/bin/env python

import SOAPpy
import sys
from optparse import OptionParser
 
def f5Conn(options):
    iControlUrl = "https://%s:%s@%s/iControl/iControlPortal.cgi" %(options.bigipUsername,options.bigipPassword,options.bigipHost)
    iControlVirtualServerNamespace = "urn:iControl:LocalLB/VirtualServer"
    vsProxy = SOAPpy.SOAPProxy(iControlUrl, iControlVirtualServerNamespace)
    return vsProxy
 
def addRuleToVirtualServer(vsProxy, rule, priority, vs):
    ruleStructure = dict(rule_name=rule, priority=priority)
    for item in vs:
        try:
            res = vsProxy.add_rule(
                virtual_servers=[item],
                rules = [[ruleStructure]]
            )
        except:
           return sys.exc_info()[1]
 
def removeRuleFromVirtualServer(vsProxy, delRule, ruleList):
    for ruleListItem in ruleList:
        for (vserver,rules) in ruleListItem:
            for (a,b) in rules:
                    if (delRule in a):
                        ruleStructure = dict(rule_name=a, priority=b)
                        try:
                            res = vsProxy.remove_rule(
                                virtual_servers=[vserver],
                                rules = [[ruleStructure]]
                            )
                        except:
                            return sys.exc_info()[1]
 
if __name__ == ' __main__':
    foo

```

---

<div class="post-metadata">

**Author:** ![L4L7\_53191](https://avatars.discourse-cdn.com/v4/letter/l/f475e1/32.png) [@L4L7\_53191](https://community.f5.com/u/L4L7_53191)\
**Post date:** [October 1, 2012, 3:11am UTC](https://community.f5.com/t/adding-a-irule-to-a-virtual-server/35340/5 "2012-10-01T03:11:02Z")

</div>

Guys I will pull together an example of how to do this. It’ll be Wednesday before I can get to it though…

--Matt

---

<div class="post-metadata">

**Author:** ![Thomas\_Weisshaa](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@Thomas\_Weisshaa](https://community.f5.com/u/Thomas_Weisshaa)\
**Post date:** [October 1, 2012, 8:04pm UTC](https://community.f5.com/t/adding-a-irule-to-a-virtual-server/35340/6 "2012-10-01T20:04:55Z")

</div>

Hi again,

thanks to Matt’s hint I figured it out - it’s a serialization issue:

Your convert\_rule() should look like this:

```tcl
ruleSequence = bigip.LocalLB.VirtualServer.typefactory.create('LocalLB.VirtualServer.VirtualServerRuleSequence')
ruleSet = bigip.LocalLB.VirtualServer.typefactory.create('LocalLB.VirtualServer.VirtualServerRule')
ruleSet.rule_name = ruleName
ruleSet.priority = priority
ruleSequence.item = [ruleSet]
return [ruleSequence]

```

And then execute it like:

bigip.LocalLB.VirtualServer.add\_rule(virtual\_servers = [‘/Common/VS’], rules = [convert\_rule(bigip, ‘/Common/test’, ‘’)])
