# Use XML module for insert lines

**URL:** <https://forum.ansible.com/t/use-xml-module-for-insert-lines/1208>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [September 29, 2023, 8:58pm UTC](https://forum.ansible.com/t/use-xml-module-for-insert-lines/1208 "2023-09-29T20:58:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shlomco](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@shlomco](https://forum.ansible.com/u/shlomco)\
**Post date:** [September 29, 2023, 8:58pm UTC](https://forum.ansible.com/t/use-xml-module-for-insert-lines/1208/1 "2023-09-29T20:58:56Z")

</div>

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](http://google.com)  
‘’’  
But the results no like as I expected the lines add in bottom of file like :  
‘’’  
  
invalidate.session.exclude.list

‘’’

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [September 30, 2023, 10:23am UTC](https://forum.ansible.com/t/use-xml-module-for-insert-lines/1208/2 "2023-09-30T10:23:58Z")

</div>

Hello @shlomco, welcome to the Community!

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

```yaml
- 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
<?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>

```

```auto
$ 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](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](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_templating.html)

Hope it helps!
