# need to replace a line in config file

**URL:** <https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940>\
**Category:** Ansible Project\
**Created:** [November 5, 2018, 6:21am UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940 "2018-11-05T06:21:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 5, 2018, 6:21am UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/1 "2018-11-05T06:21:17Z")

</div>

Hi Experts,

Please help me with your guidance here.

I want to comment a line in one config file and need to uncomment the other lline using ansible.

My file content as below.

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 5, 2018, 11:34am UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/2 "2018-11-05T11:34:34Z")

</div>

Hi Experts,

Any suggestion here ?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 5, 2018, 1:50pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/3 "2018-11-05T13:50:27Z")

</div>

Hi

I would not use lineinfile and regular expressions to manipulate XML.  
You might get it to work, but it will be very fragile.  
Instead have a look at the xml module, which (if you can get it to  
work) will be more robust:

[https://docs.ansible.com/ansible/2.4/xml\_module.html](https://docs.ansible.com/ansible/2.4/xml_module.html)

Dick

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 5, 2018, 1:56pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/4 "2018-11-05T13:56:03Z")

</div>

> Hi Experts,
> 
> Any suggestion here ?

People do work in different timezone and/or are busy so you cant expect an answer one the hour.

> > My file content as below.
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<include location="conf/basicUserRegistry.xml"/\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-------------------------this i want to comment  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<!--include location="conf/ldapUserRegistry.xml"/--\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--------------------------- this line i want to  
> > uncomment
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Note the / on the end.

> > ---------------------------------------------------------------------------------------------------------------------------------------------------
> > 
> > - name: enable LDAP  
> > &nbsp;&nbsp;hosts: localhost  
> > &nbsp;&nbsp;tasks:  
> > &nbsp;&nbsp;&nbsp;- name: disbale basic user-registry  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path:  
> > /opt/IBM/605CLM/JazzTeamServer/server/liberty/servers/clm/server.xml  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^\s\*\<include location="conf/basicUserRegistry.xml"\>.\*$'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '\<!--include location="conf/basicUserRegistry.xml"--\>;'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;become: true
> > 
> > -----------------------------------------------------------------------------------------------------------------------------------------------------------  
> > ---
> > 
> > - name: enable LDAP USER\_REGISTRY  
> > &nbsp;&nbsp;hosts: localhost  
> > &nbsp;&nbsp;tasks:  
> > &nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest:  
> > /opt/IBM/605CLM/JazzTeamServer/server/liberty/servers/clm/server.xml  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^(.\*)\<!--include  
> > location="conf/ldapUserRegistry.xml"--\>(.\*)$'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '\<include location="conf/ldapUserRegistry.xml"\>;'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backrefs: yes

Both of you regexp is missing a / at the end

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 5, 2018, 2:45pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/5 "2018-11-05T14:45:15Z")

</div>

Hello Kai,

Thanks a lot for your gudance here. Its really appreciated.

I am very new to ansible and thought like this is a normal task already done by experts like you and was eager to get a reply on that…

Here i just gone through the xml module and couldnt dfind any information to replace lines as we seen in the lineinfile module. Please correct me if i missed any…

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 5, 2018, 3:46pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/6 "2018-11-05T15:46:19Z")

</div>

Hi Visar

Correct.  
The point I was trying to make is that using string manipulation to  
edit an XML configuration file might not be the best way to go about  
it.  
A regular expression might work - until it doesn't. For instance when  
the content is split over lines, uses inconsistent spacing, the regex  
eats multiple instances which you didn't want, etc.  
The XML module does not "replace lines" , but will operate on the  
actual XML elements, which is more robust.

For this to work you need to know the structure of the XML - can you  
supply the server.xml?

Thanks

Dick

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 5, 2018, 3:58pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/7 "2018-11-05T15:58:57Z")

</div>

Hello kai,

This is the xml file , where i want to enable Ldap user registry and disable basic local registry using ansible

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 5, 2018, 6:54pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/8 "2018-11-05T18:54:04Z")

</div>

Your XML file actually has multiple include locations, so you'd have  
to loop through them.  
Since this is the case, you also need to make sure there are no  
additional include locations.  
Something like this should do the trick:

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;locations:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- conf/ldapUserRegistry.xml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- conf/application.xml

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: Ensure locations are configured  
&nbsp;&nbsp;&nbsp;&nbsp;xml:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: server.xml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xpath: "/server/include[{{ i + 1 }}]"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attribute: location  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ locations }}"  
&nbsp;&nbsp;&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index\_var: i

&nbsp;&nbsp;- name: Ensure no other locations are configured  
&nbsp;&nbsp;&nbsp;&nbsp;xml:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: server.xml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xpath: "/server/include[position()\>{{ (locations | length) }}]"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 9, 2018, 12:27pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/9 "2018-11-09T12:27:05Z")

</div>

Hello Visser,

Appreciate your time for checking this.

Here i am little confused that,

Here I just want to modify the server.xml file only.  
(comment basicuserregistry.xml and uncomment ldapuseruserregistry.xml file).

Both the files are already present there in the defined location already, but just want to edit the server.xml file to comment and uncomment…

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 12, 2018, 2:53pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/10 "2018-11-12T14:53:55Z")

</div>

I tried with below play book but its not changing anything but playbook is running without any issues.

```auto
- name: LDAP Settings
 hosts: myserver
 vars:
   mark_1: 'include location="conf/basicUserRegistry.xml"/'
   mark_2: 'include location="conf/ldapUserRegistry.xml"/'
 tasks:
    - replace:
       path: /opt/IBM/server/liberty/servers/clm/server.xml
       regexp: "^<!--{{ mark_2 }}-->"
       replace: "<{{ mark_2 }}>"
    - replace:
       path: /opt/IBM/server/liberty/servers/clm/server.xml
       regexp: "^<{{ mark_1 }}>"
       replace: "<!--{{ mark_1 }}-->"
       backup: yes'

```

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 12, 2018, 4:14pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/11 "2018-11-12T16:14:11Z")

</div>

It would rather see the appreciation for my time by trying out the  
playbook that I sent.  
Because there is a very good chance that it will solve your problem.  
Please let me know how it went.

Dick

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 12, 2018, 5:23pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/12 "2018-11-12T17:23:39Z")

</div>

Hello Dick,

Yes, You are right that the given play is working .

The playbook is making changes on the xml file, but not as expected. Its making double entry for same ldap settings. one is commented another is uncommented.

But here I want a result like comment only the basic user registry line and uncomment ldap line

here the basic registry line itself got removed…

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 12, 2018, 5:32pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/13 "2018-11-12T17:32:06Z")

</div>

Hello Dick,

Now this is the output.

Here ldapUserRegistry line is repeating twice where one should be basicUserRegistry xml file which should be commented…

Thanks in advance for your help here

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 12, 2018, 6:05pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/14 "2018-11-12T18:05:47Z")

</div>

Hi Visar

You keep thinking in terms of "commenting out lines" in your XML  
configuration file.  
But what you really want (although you might not realise it), is to  
configure your application server the right way.  
This playbook does exactly that.  
Ignore what is commented out - like your application server does.

At this moment you're all set!

Dick

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 12, 2018, 7:16pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/15 "2018-11-12T19:16:15Z")

</div>

Hi Dick,

By editing this configuration xml file I am disabling the default basicregistry file user authentication which is local and I am enabling the LDAP setting by commenting out. Once the LDAP line got uncommented I have my own LDAP settings in the ldapuser registry file. Also in some situations where LDAP is not working I want to use the default basic user registry as well.

That’s why I am looking for enabling LDAP file and disabling basicregistry file here.

In the given playbook ldapuser registry is enabled and disabled in different lines as I given in prevy example

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 13, 2018, 11:17am UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/16 "2018-11-13T11:17:12Z")

</div>

Hi Vivek

It seems that the basics of XML and system configuration are not clear.  
There is no use in continuing this conversation for me.

Regards

Dick

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 13, 2018, 12:23pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/17 "2018-11-13T12:23:44Z")

</div>

Hello Visser,

I am sorry if i couldn’t explain you the way which i was looking for here.

Please find the default server.xml file which is by default pointing to the “basicUserRegistry.xml” file located in applications conf directory. Here in order to proceed further installation step of our application, we have to enable “ldapUserRegistry.xml” file in the same directory. Because we cant proceed the further steps with “basicUserRegistry” user. We have already “ldapUserRegistry.xml” file edited with our domain properties and it will be replaced by another play book in the same applications conf directory.

Now our requirement is to just enable the “ldapUserRegistry.xml” file and disable “basicUserRegistry.xml” ( We dont want to remove this parameter because in some troubleshooting scenarios, we can authenticate the application with basic user registry and check the things, where ldapuser is failing)

here nothing else we want to modify in this file as mistake in this file modification will result the application down…

---

<div class="post-metadata">

**Author:** ![Maria\_MacCallum](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Maria\_MacCallum](https://forum.ansible.com/u/Maria_MacCallum)\
**Post date:** [November 13, 2018, 4:23pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/18 "2018-11-13T16:23:32Z")

</div>

Dear Visar,

By no means am I an ansible expert but I thought of 2 ideas around the problem for you:

1. Have 2 files on the server you can copy between the 2 files to be the ‘active’ file using ansible, one with the commented out section, one with it active

- When you have changes to make to the xml file use ansible to copy the new ones to the server

1. If you are using unix, you can use shell to use sed to search and replace.

Hope this helps in the interim while you find a proper ansible solution to your problem

Regards, Maria

---

<div class="post-metadata">

**Author:** ![Vivek1](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@Vivek1](https://forum.ansible.com/u/Vivek1)\
**Post date:** [November 13, 2018, 5:31pm UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/19 "2018-11-13T17:31:08Z")

</div>

Thanks Maria,

I will check for that. Do u have any scenario like where you could modify the xml file with see command and how we can automate this using ansible…

---

<div class="post-metadata">

**Author:** ![Maria\_MacCallum](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Maria\_MacCallum](https://forum.ansible.com/u/Maria_MacCallum)\
**Post date:** [November 14, 2018, 10:00am UTC](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940/20 "2018-11-14T10:00:18Z")

</div>

Hello,

Here’s a cobbled together example from a script of mine, sed uses regular expressions but can be slightly different how it works depending on your 'nix ‘flavour’, this comments out a specific line:

sed -e “s/\<name\>pkg://puppetlabs.com/puppet-agent\</name\>/\<!– \<name\>pkg:/system/management/puppet\</name\> –\>/” TEMPLATE-FILENAME \> FINAL-FILENAME

If I break it down a bit:

sed -e "s

# 1st forward slash means the search field is next

/

# have to escape lots of chars with backslashes

\<name\>pkg://puppetlabs.com/puppet-agent\</name\>

# next forward slash is for the replace field

/

\<!– \<name\>pkg:/system/management/puppet\</name\> –\>

# ends with a forward slash and quotes

/"

# Then the source filename comes next

TEMPLATE-FILENAME

# then however your shell can pipe to a new file

> 

# Then your final filename

FINAL-FILENAME

Do lots of local testing 1st!!!

Regards, Maria

[Next page](https://forum.ansible.com/t/need-to-replace-a-line-in-config-file/28940.md?page=2)
