# Create an iRule that can search and replace

**URL:** <https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185>\
**Category:** Technical Forums\
**Tags:** irules, soap\
**Created:** [June 7, 2025, 11:22am UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185 "2025-06-07T11:22:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rg-f5](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rg-f5](https://community.f5.com/u/rg-f5)\
**Post date:** [June 7, 2025, 11:22am UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/1 "2025-06-07T11:22:13Z")

</div>

Looking for some irule help please this is a search and replace rule…

**Step one** in the message payload

**Find** soapenv:Header/ **and replace with**

soapenv:Header  
job:roleIdentifier_ **SERVER1** _\</job:roleIdentifier\>  
\</soapenv:Header\>

or if the message is from Server2

**Find** soapenv:Header/ **and replace with**

soapenv:Header  
job:roleIdentifier_ **S**** ERVER2**_\</job:roleIdentifier\>  
\</soapenv:Header\>

---

<div class="post-metadata">

**Author:** ![Injeyan\_Kostas](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/injeyan_kostas/32/263_2.png) [@Injeyan\_Kostas](https://community.f5.com/u/Injeyan_Kostas)\
**Post date:** [June 7, 2025, 7:43pm UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/2 "2025-06-07T19:43:56Z")

</div>

Assuming that you can identify server1 and 2 based on IP you can use below exmaple

```tcl
when HTTP_REQUEST 
{
	switch [IP::client_addr] 
	{
        "server1_IP" 
		{
            set server "SERVER1"
			HTTP::collect
        }
        "server2_IP" 
		{
            set server "SERVER2"
			HTTP::collect
        }
	}
}

when HTTP_REQUEST_DATA 
{
	if {[info exists server]} 
	{
		set payload [HTTP::payload]
		set new_header "<soapenv:Header><job:roleIdentifier>$server</job:roleIdentifier></soapenv:Header>"
		regsub {<soapenv:Header/>} $payload $new_header payload
		HTTP::payload replace 0 [string length [HTTP::payload]] $payload
	}
}

```

---

<div class="post-metadata">

**Author:** ![rg-f5](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rg-f5](https://community.f5.com/u/rg-f5)\
**Post date:** [June 8, 2025, 7:14am UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/3 "2025-06-08T07:14:02Z")

</div>

thank you for your message, when you say identify the server, how can identify the server with irule code?

what if i just wanted to replace the below and then assign the irule to the virtual server, then i would have identified which server the message came from.

**Step one** in the message payload

**Find** soapenv:Header/ **and replace with**

soapenv:Header  
job:roleIdentifier_ **SERVER1** _\</job:roleIdentifier\>  
\</soapenv:Header\>

---

<div class="post-metadata">

**Author:** ![Injeyan\_Kostas](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/injeyan_kostas/32/263_2.png) [@Injeyan\_Kostas](https://community.f5.com/u/Injeyan_Kostas)\
**Post date:** [June 8, 2025, 7:48am UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/4 "2025-06-08T07:48:54Z")

</div>

It depends how is your topology.

If you you a specific VS fo each server then you don’t need to identify it.

But if you have one VS then you somehow need to know which server it is.

You can use IPs, headers, cookies, or whatever unique application may has.

---

<div class="post-metadata">

**Author:** ![Injeyan\_Kostas](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/injeyan_kostas/32/263_2.png) [@Injeyan\_Kostas](https://community.f5.com/u/Injeyan_Kostas)\
**Post date:** [June 8, 2025, 7:51am UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/5 "2025-06-08T07:51:14Z")

</div>

Another one is that you don’t specify if this is a request or a response.

So my example is for request.

If you need it for a response it needs to be adjusted accordingly.

---

<div class="post-metadata">

**Author:** ![rg-f5](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rg-f5](https://community.f5.com/u/rg-f5)\
**Post date:** [June 8, 2025, 2:32pm UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/6 "2025-06-08T14:32:47Z")

</div>

The message will come through from an application to the Virtual Server, i’m not sure if this is a response or a request. The message will not have a role identifier. Hence I have to add in the following irule.

Let’s start with it being one virtual server and we want a basic example to get the irule started to search and replace.

This is a message sent from the application, and an irule to replace soapenv:Header/ with soapenv:Header  
job:roleIdentifier_ **SERVER1** _\</job:roleIdentifier\>  
\</soapenv:Header\>

**Step one** in the message payload

**Find** soapenv:Header/ **and replace with**

soapenv:Header  
job:roleIdentifier_ **SERVER1** _\</job:roleIdentifier\>  
\</soapenv:Header\>

---

<div class="post-metadata">

**Author:** ![rg-f5](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rg-f5](https://community.f5.com/u/rg-f5)\
**Post date:** [June 9, 2025, 7:59pm UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/7 "2025-06-09T19:59:15Z")

</div>

Server 1 and Server 2 are both separate. I thought the irule can be attached to each server.

---

<div class="post-metadata">

**Author:** ![Injeyan\_Kostas](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/injeyan_kostas/32/263_2.png) [@Injeyan\_Kostas](https://community.f5.com/u/Injeyan_Kostas)\
**Post date:** [June 10, 2025, 4:47am UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/8 "2025-06-10T04:47:07Z")

</div>

Oh when you say server you mean virtual server?

If so yes you can attach same irule to both virtual servers.

But again you somehow need to identify which virtual server was used.

You need to modify the request or the response?

---

<div class="post-metadata">

**Author:** ![Injeyan\_Kostas](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/injeyan_kostas/32/263_2.png) [@Injeyan\_Kostas](https://community.f5.com/u/Injeyan_Kostas)\
**Post date:** [June 10, 2025, 9:33am UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/9 "2025-06-10T09:33:56Z")

</div>

So for virtual servers you can use [IP::local\_addr clientside] instead of [IP::client\_addr]

---

<div class="post-metadata">

**Author:** ![rg-f5](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rg-f5](https://community.f5.com/u/rg-f5)\
**Post date:** [June 10, 2025, 2:08pm UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/10 "2025-06-10T14:08:56Z")

</div>

it’s a request. And they will hit individual VIP’s (Virtual Servers).

---

<div class="post-metadata">

**Author:** ![Injeyan\_Kostas](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/injeyan_kostas/32/263_2.png) [@Injeyan\_Kostas](https://community.f5.com/u/Injeyan_Kostas)\
**Post date:** [June 10, 2025, 3:37pm UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/11 "2025-06-10T15:37:54Z")

</div>

then you can try to apply this irule to both Virtual Servers

```tcl
when HTTP_REQUEST 
{
	switch [IP::local_addr clientside] 
	{
        "VIP_1" 
		{
            set server "SERVER1"
        }
        "VIP_2" 
		{
            set server "SERVER2"
        }
	}
	if {[info exists server]} 
	{
        if {[HTTP::header "Content-Length"] ne "" && [HTTP::header "Content-Length"] <= 1048576}
        {
            set content_length [HTTP::header "Content-Length"]
	    } 
	    else 
	    {
		    set content_length 1048576
	    }
    }
    if { $content_length > 0} 
    {
        HTTP::collect $content_length
    }
}

when HTTP_REQUEST_DATA 
{
	if {[info exists server]} 
	{
		set payload [HTTP::payload]
	    set new_header "<soapenv:Header><job:roleIdentifier>$server</job:roleIdentifier></soapenv:Header>"
	    regsub {<soapenv:Header/>} $payload $new_header payload
	    HTTP::payload replace 0 [string length [HTTP::payload]] $payload
	}
}

```

---

<div class="post-metadata">

**Author:** ![rg-f5](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@rg-f5](https://community.f5.com/u/rg-f5)\
**Post date:** [June 11, 2025, 9:17am UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/12 "2025-06-11T09:17:30Z")

</div>

Thank you, what fields would i edit for server ip or name? I’m thinking Server1 and Server2.

I’ll give this a test on the irule.

What can i do if the message sent over doesn’t have a soapheader and i need to insert one in?

---

<div class="post-metadata">

**Author:** ![Injeyan\_Kostas](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/injeyan_kostas/32/263_2.png) [@Injeyan\_Kostas](https://community.f5.com/u/Injeyan_Kostas)\
**Post date:** [June 11, 2025, 9:23am UTC](https://community.f5.com/t/create-an-irule-that-can-search-and-replace/76185/13 "2025-06-11T09:23:41Z")

</div>

VIP\_1 and VIP\_2 should be change with virtual servers IPs  
Server1 and Server2 can be changed to whatever you need to add in payload
