title: Ansible xml manipulation similar to lineinfile
body: <p>In Ansible, I'm looking for a technique that works similar to <code>lineinfile</code> or <code>replace</code> but for XML files when using templates is not an option. Seems like a very common need.</p>
  
  <p>With XML files, though, it is necessary to specify an <code>xpath</code> to guarantee the element is present/absent from the correct place in the DOM. </p>
  
  <p>The solution needs to ensure there is a mechanism of replacing an existing node that <em>could</em> look quite a bit different than the target node.</p>
  
  <p>Trivial example XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;datasources-configuration xmlns:myns="http://org.someorg.ds/config"&gt;
    &lt;datasources&gt;
      &lt;!-- various other xml --&gt;
      &lt;datasource&gt;
          &lt;name&gt;MyDS&lt;/name&gt;
          &lt;jdbcUrl&gt;...&lt;/jdbcUrl&gt;
      &lt;/datasource&gt;
    &lt;/datasources&gt;
    &lt;!-- various other xml --&gt;
  &lt;/datasources-configuration&gt;
  </code></pre>
  
  <p>I want to be able to ensure a full multiline block of XML gets inserted/replaced into target XML file given a certain xpath expression is matched. For example, to add the following datasource to datasources:</p>
  
  <pre><code>   &lt;datasource&gt;
        &lt;name&gt;AnotherDS&lt;/name&gt;
        &lt;jdbcUrl&gt;...&lt;/jdbcUrl&gt;
     &lt;/datasource&gt;
  </code></pre>
  
  <p>The best that I've seen is this custom module which breaks on it's own examples: <a href="https://github.com/cmprescott/ansible-xml">https://github.com/cmprescott/ansible-xml</a></p>
  
  <p>Does such a module exist or solution recommendations?</p>