# Replacing a block with another block

**URL:** <https://forum.ansible.com/t/replacing-a-block-with-another-block/44500>\
**Category:** Get Help\
**Created:** [September 16, 2025, 4:59pm UTC](https://forum.ansible.com/t/replacing-a-block-with-another-block/44500 "2025-09-16T16:59:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerochaz](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@aerochaz](https://forum.ansible.com/u/aerochaz)\
**Post date:** [September 16, 2025, 4:59pm UTC](https://forum.ansible.com/t/replacing-a-block-with-another-block/44500/1 "2025-09-16T16:59:53Z")

</div>

I’m trying to add the following lines after redirectPort=“8443”:  
proxyName=“[mywebsitename.com](http://mywebsitename.com)”  
proxyPort=“443”  
scheme=“https”\>  
However, there are multiple lines of redirectPort=“8443” in the document, so if I use the blockinfile option, it adds those lines to the wrong section of the document.

The section I want to add it to looks like this:

```
    <Connector
            port="1234"
            protocol="HTTP/1.1"

            maxThreads="150" minSpareThreads="25"
            connectionTimeout="20000"
            disableUploadTimeout="true"
            acceptCount="100"

            enableLookups="false"
            maxHttpHeaderSize="8192"

            useBodyEncodingForURI="true"
            URIEncoding="UTF-8"

            redirectPort="8443">

```

The only defining feature between this section and other sections is the Service name = “Catalina”. I thought maybe I could replace this entire Section with the same section, but with additional lines, but I’m having trouble doing that. Is it possible to replace an entire block with another block of lines when everything inside of the block repeats in the document?

This is what I want it to look like:

```
    <Connector
            port="1234"
            protocol="HTTP/1.1"

            maxThreads="150" minSpareThreads="25"
            connectionTimeout="20000"
            disableUploadTimeout="true"
            acceptCount="100"

            enableLookups="false"
            maxHttpHeaderSize="8192"

            useBodyEncodingForURI="true"
            URIEncoding="UTF-8"

            redirectPort="443"
            proxyName="mywebsitename.com"
            proxyPort="443"
            scheme="https">

```

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [September 17, 2025, 5:25am UTC](https://forum.ansible.com/t/replacing-a-block-with-another-block/44500/2 "2025-09-17T05:25:51Z")

</div>

You might want to take a look at the [community.general.xml module](https://docs.ansible.com/ansible/latest/collections/community/general/xml_module.html). That might be better suited for this. (I’ve never used it, so no idea how well or not it will work for this.)

---

<div class="post-metadata">

**Author:** ![aerochaz](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@aerochaz](https://forum.ansible.com/u/aerochaz)\
**Post date:** [September 17, 2025, 2:25pm UTC](https://forum.ansible.com/t/replacing-a-block-with-another-block/44500/3 "2025-09-17T14:25:54Z")

</div>

I think the problem I’m having is with my regexp. I am taking two lines out as a sample and using grep -P to find the lines in the file and it’s not returning any results. For whatever reason it doesn’t like something like regexp: ‘URIEncoding=“UTF-8”\s\*\n\s\*redirectPort=“8443”\>’  
If I do the URIEncoding separately, it finds that. If I do the redirectPort separately it finds that, but it doesn’t find them when I add it together with a \n.
