Use XML module for insert lines

Hi folks

I some help , I have xml as following structure
‘’’



login.url
/login.action?os_destination=${originalurl}&permissionViolation=true


link.login.url
/login.action


cookie.encoding
cNf


login.cookie.key
seraph.confluence


login.submit.url
/dologin.action

    <!--only basic authentication available-->
    <init-param>
        <param-name>authentication.type</param-name>
        <param-value>os_authType</param-value>
    </init-param>

    <!-- Invalidate session on login to prevent session fixation attack -->
     <init-param>
        <param-name>invalidate.session.on.login</param-name>
        <param-value>true</param-value>
    </init-param>
    <!-- Add names for session attributes that must not be copied to a new session when the old one gets invalidated.
      Currently it is empty (i.e. all attributes will be copied). -->
    <init-param>
        <param-name>invalidate.session.exclude.list</param-name>
        <param-value></param-value>
    </init-param>
</parameters>

‘’’

I need to insert the following lines inside

    <init-param>
        <param-name>logout.url</param-name>
        <param-value>http://google.com</param-value>
    </init-param>

I tried the following code
‘’’
- name: Test 1
community.general.xml:
path: config.xml
xpath: /security-config/parameters/init-param
add_children:
- init-param:
param-name: logout.url
param-value: http://google.com
‘’’
But the results no like as I expected the lines add in bottom of file like :
‘’’

invalidate.session.exclude.list

‘’’

1 Like

Hello @shlomco, welcome to the Community!

I was able to obtain the results you expect with this new version of your code:

- name: Forum PB
  hosts: localhost
  gather_facts: false
  tasks:

    - name: Task 1.1
      community.general.xml:
        path: debug/xml/config.xml
        xpath: /security-config/parameters
        add_children:
          - init_param: ""

    - name: Task 1.2
      community.general.xml:
        path: debug/xml/config.xml
        xpath: /security-config/parameters/init_param
        add_children:
          - param-name: "logout.url"

    - name: Task 1.3
      community.general.xml:
        path: debug/xml/config.xml
        xpath: /security-config/parameters/init_param
        add_children:
          - param-value: "http://google.com"

Results:

<?xml version='1.0' encoding='UTF-8'?>
<security-config>
    <parameters>
        <!--only
        basic authentication available-->
        <init-param>
            <param-name>authentication.type</param-name>
            <param-value>os_authType</param-value>
        </init-param>
        <!-- Invalidate session on login to prevent session fixation attack -->
        <init-param>
            <param-name>invalidate.session.on.login</param-name>
            <param-value>true</param-value>
        </init-param>
        <!-- Add names for session attributes that must not be copied to a new session when the old
        one gets invalidated. Currently it is empty (i.e. all attributes will be copied). -->
        <init-param>
            <param-name>invalidate.session.exclude.list</param-name>
            <param-value />
        </init-param>
        <!-- NEW PARAMS -->
        <init_param>
            <param-name>logout.url</param-name>
            <param-value>http://google.com</param-value>
        </init_param>
    </parameters>
</security-config>
$ ansible-playbook -i localhost fourm_pb.yml
PLAY [Forum PB] ****************************************************************************

TASK [Task 1.1] ****************************************************************************
changed: [localhost]

TASK [Task 1.2] ****************************************************************************
changed: [localhost]

TASK [Task 1.3] ****************************************************************************
changed: [localhost]

PLAY RECAP *********************************************************************************
localhost                  : ok=3    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Just play around with it, you might add more than one children at once:

https://docs.ansible.com/ansible/latest/collections/community/general/xml_module.html#parameter-add_children

Another approach to achieve this would be using jinja2 templating:

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_templating.html

Hope it helps!

2 Likes