Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create multiple nodes? #117

Closed
KAMI911 opened this issue Jun 7, 2017 · 8 comments · Fixed by ansible/ansible#25323
Closed

How to create multiple nodes? #117

KAMI911 opened this issue Jun 7, 2017 · 8 comments · Fixed by ansible/ansible#25323
Labels

Comments

@KAMI911
Copy link

KAMI911 commented Jun 7, 2017

Hi,

This is a support question (or feature request), rather than error report but how can I create structure like:

    <error-page>
        <error-code>404</error-code>
        <location>/error/404.html</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/error/500.html</location>
    </error-page>

I have tried several ideas from with_items lists to add_children, but I was not able to crate such structure. I am using latest Ansible with latest xml library that is available here.

Thank you in advance!

@sm4rk0
Copy link
Collaborator

sm4rk0 commented Jun 8, 2017

Is this what you want to achieve?

- hosts: localhost
  roles:
    - cmprescott.xml
  tasks:
    - name: Issue #117
      xml:
        file: test.xml
        xpath: /root
        add_children: "{{ item }}"
        input_type: xml
      with_items:
        - <error-page><error-code>404</error-code><location>/error/404.html</location></error-page>
        - <error-page><error-code>500</error-code><location>/error/500.html</location></error-page>

Content of test.xml before running the play:

<root/>

Content of test.xml after running the play (manually reformatted):

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <error-page>
    <error-code>404</error-code>
    <location>/error/404.html</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/error/500.html</location>
  </error-page>
</root>

@KAMI911
Copy link
Author

KAMI911 commented Jun 8, 2017

Hi,
Thank you for the quick answer. That is what I wanted, however I hit a problem during I built this into my scripts.

I got OK from the task but the file is not changed. I am using it to modify web.xml of Tomcat, but nothing happen. Code is here:
https://github.com/KAMI911/ansible-role-tomcat/blob/cce54f2979c9d2c41080e142df6e20e52d56d83b/tasks/custom-errorpages-tomcat.yml
Variables from here:
https://github.com/KAMI911/ansible-role-tomcat/blob/39aad223177a1e29790e7df972630ddadf0a1af2/vars/main.yml

Output of one iteration item:

ok: [maserver.local] => (item=<error-page><error-code>504</error-code><location>/errorpages/504.jsp</location></error-page>) => {
     "actions": {
         "ensure": "present",
         "namespaces": {},
         "xpath": "/web-app"
     },
     "changed": false,
     "count": 0,
     "invocation": {
         "module_args": {
             "add_children": [
                 "<error-page><error-code>504</error-code><location>/errorpages/504.jsp</location></error-page>"
             ],
             "attribute": null,
             "content": null,
             "count": null,
             "ensure": "present",
             "file": "/opt/tomcat/tomcat/conf/web.xml",
             "input_type": "xml",
             "namespaces": {},
             "pretty_print": false,
             "print_match": null,
             "set_children": null,
             "value": null,
             "xmlstring": "",
             "xpath": "/web-app"
         },
         "module_name": "xml"
     },
     "item": "<error-page><error-code>504</error-code><location>/errorpages/504.jsp</location></error-page>",
     "matches": [],
     "msg": ""
 }

Do you have idea what did I wrong?

@sm4rk0
Copy link
Collaborator

sm4rk0 commented Jun 8, 2017

As I suspected, the problem is due to XML namespaces. You need to specify the namespaces argument to the xml module and use that namespace in xpath argument if there is an xmlns attribute in XML:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" ...

So, add this argument:

namespaces:
  j2ee: http://java.sun.com/xml/ns/j2ee

and use this value for xpath argument:

xpath:  /j2ee:web-app

@KAMI911
Copy link
Author

KAMI911 commented Jun 8, 2017

Thank you Sir!
I did not realized... :/ It is working perfectly. Probably a link from a README.md to test may help newcomers how to use this feature.

@KAMI911
Copy link
Author

KAMI911 commented Jun 8, 2017

I have only one problem: it is always update the xml file, so if I run twice it adds all element twice :/

@sm4rk0
Copy link
Collaborator

sm4rk0 commented Jun 8, 2017

Yes, that's known issue. See #7, #35 and #42.
I've proposed adding an issue template about the namespace catch. Lack of documentation should be resolved soon because the module is being added to "ansible core".

@dagwieers
Copy link
Collaborator

dagwieers commented Aug 9, 2017

The xml module is upstream now (and ships with Ansible v2.4).

I am interested in improvements to the documentation upstream. You can open a PR with specific suggestions and we'll get them merged.

Please close this issue, and if needed, open a new issue at: https://github.com/ansible/ansible/issues

@sm4rk0
Copy link
Collaborator

sm4rk0 commented Aug 13, 2017

@KAMI911, would you please close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants