Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

Set xml node attribute using ansible xml module

I have the following xml file:

<background>
    <starttime>
      <year>2021</year>
      <month>01</month>
      <day>22</day>
      <hour>15</hour>
      <minute>59</minute>
      <second>54</second>
    </starttime>
    <static>
        <duration>10000000000.0</duration>
        <file>
            <size width="1" height="1">A</size>
        </file>
    </static>
</background>

I would like to set the 'width' attribute of the 'size' node. So far I have the following ansible code:

- name: Set the attribute 'width' of size
  xml:
    path: "some.xml"
    xpath: "{{ item.xpath }}"
    value: "{{ item.value }}"
  with_items:
    - { xpath: "/background/static/file/size/@width", value: "1920" }

This gives me the error:

"Xpath /background/static/file/size/@width does not reference a node!"

I saw similar questions, but none had helped me. I am pretty sure that my xpath expression is correct. My question, is how to set the attribute using ansible xml?

question from:https://stackoverflow.com/questions/65887279/set-xml-node-attribute-using-ansible-xml-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found the solution:

- name: Set the attribute 'width' of size
  xml:
    path: "some.xml"
    xpath: "{{ item.xpath }}"
    attribute: "{{ item.attribute }}"
    value: "{{ item.value }}"
  with_items:
    - { xpath: "/background/static/file/size", attribute: "width", value: "1920" }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...