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

Wrong insertion of yml object #101

Closed
stncrn opened this issue Feb 21, 2017 · 5 comments
Closed

Wrong insertion of yml object #101

stncrn opened this issue Feb 21, 2017 · 5 comments
Labels

Comments

@stncrn
Copy link

stncrn commented Feb 21, 2017

Hello, I am having troubles inserting a complete block in an xml file.

Let's consider I have this simplified XML :

<List>
    <Item id="id001">
        <Label>Demo Label</Label>
        <Active>1</Active>
    </Item>
</List>

I want to add another Item into it, as List child. So the XML should become :

<List>
    <Item id="id001">
        <Label>Demo Label</Label>
        <Active>1</Active>
    </Item>
    <Item id="id002">
        <Label>Demo 2</Label>
        <Active>0</Active>
    </Item>
</List>

So I used different web XML2YML services to convert the addtionnal XML block to a YML object, all of them giving me the same result.
My playbook task is therefore looking like this :

  - name: Config xml
    xml: 
        file: /tmp/export.xml
        xpath: /List
        add_children: 
          - Item:
              _Id: id002
              Label: "Demo 2"
              Active: "0"

But the result is not the one expected :

<List>
    <Item id="id001">
        <Label>Demo Label</Label>
        <Active>1</Active>
    </Item>
    <Item id="id002" Label="Demo 2" Active="0"></Item>
</List>

When trying to insert a "bigger" object, with several childs an sub-childs, which is what I definitely need, I get this error :
TypeError: Argument must be string or unicode.

As the blocks I need to insert have several levels of subchilds, for around 20~30 lines, I would like to avoid using add_children each time to create each child one by one.

@dagwieers
Copy link
Collaborator

dagwieers commented May 9, 2017

This is what you were looking for:

  - xml:
      file: /tmp/test155.new.xml
      xpath: /List
      pretty_print: yes
      add_children:
        - Item:
            _Id: id002
            _:
              - Label: Demo 2
              - Active: "0"

You can do it like this as an easier alternative:

  - xml:
      file: /tmp/test155.new.xml
      xpath: /List
      input_type: xml
      pretty_print: yes
      add_children:
        - "<Item id='id002'><Label>Demo 2</Label><Active>0</Active></Item>"

If you are happy with the examples, please close this issue.

@dagwieers
Copy link
Collaborator

dagwieers commented Aug 9, 2017

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

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

@stncrn
Copy link
Author

stncrn commented Aug 9, 2017

Understood. Nice to see this module shipped within Ansible !

@stncrn stncrn closed this as completed Aug 9, 2017
@juanvalino
Copy link

Hello,

Would be nice to include this examples in the official module documentation shipped with ansible. I reached this solution because I have the same problem and the solution seems not to be documented.

Thanks.

@dagwieers
Copy link
Collaborator

@juanvalino If we would include all the examples, it's like looking for the needle in the haystack :-(

If you look at the xml module integration tests, you can see what I mean.
https://github.com/ansible/ansible/tree/devel/test/integration/targets/xml/tasks
Maybe we ought to add this link to the xml module docs ?

Feel free to propose a PR with the change to the documentation you would prefer. You can click on "Edit on GitHub" to edit the documentation and make a PR. Thanks in advance !

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

No branches or pull requests

3 participants