need to replace a line in config file

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.

Hi Experts,

Any suggestion here ?

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

Dick

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.

________________________________________________________________________________________________________________________

        <include location="conf/basicUserRegistry.xml"/>
                         -------------------------this i want to comment
    <!--include location="conf/ldapUserRegistry.xml"/-->
                          --------------------------- this line i want to
uncomment

__________________________________________________________________________________________________________________________

Note the / on the end.

---------------------------------------------------------------------------------------------------------------------------------------------------

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

-----------------------------------------------------------------------------------------------------------------------------------------------------------
---

- name: enable LDAP USER_REGISTRY
  hosts: localhost
  tasks:
    lineinfile:
      dest:
/opt/IBM/605CLM/JazzTeamServer/server/liberty/servers/clm/server.xml
      regexp: '^(.*)<!--include
location="conf/ldapUserRegistry.xml"-->(.*)$'
      line: '<include location="conf/ldapUserRegistry.xml">;'
      backrefs: yes

Both of you regexp is missing a / at the end

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…

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

Hello kai,

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

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:

  vars:
    locations:
      - conf/ldapUserRegistry.xml
      - conf/application.xml

  tasks:
  - name: Ensure locations are configured
    xml:
      path: server.xml
      xpath: "/server/include[{{ i + 1 }}]"
      attribute: location
      value: "{{ item }}"
    loop: "{{ locations }}"
    loop_control:
      index_var: i

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

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…

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

- 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'

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

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…

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

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

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

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

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…

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

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…

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