Ansible:set_children不使用变量

时间:2019-04-05 12:00:37

标签: xml ansible

我正在尝试通过ansible修改一个简单的xml文件。

simple.xml

<?xml version="1.0"?>
<controller-info>
  <controller-host></controller-host>
  <controller-port></controller-port>
  <controller-ssl-enabled></controller-ssl-enabled>
</controller-info>

剧本

---
- hosts: localhost
  gather_facts: no
  vars_files:
  - xml_vars.yml
  tasks:
  - name: modify xml file
    xml:
      path: /home/user/simple.xml
      xpath: /controller-info
      set_children: "{{ controller_conf }}"

xml_vars.yml

---
controller_conf:
    - controller-host: "localhost"
    - controller-port: "443"
    - controller-ssl-enabled: "true"

运行剧本时,出现以下错误:

fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Shared connection to localhost closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/ansible/.ansible/tmp/ansible-tmp-1554463945.2-8302647655843/AnsiballZ_xml.py\", line 113, in <module>\r\n    _ansiballz_main()\r\n  File \"/home/ansible/.ansible/tmp/ansible-tmp-1554463945.2-8302647655843/AnsiballZ_xml.py\", line 105, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/home/ansible/.ansible/tmp/ansible-tmp-1554463945.2-8302647655843/AnsiballZ_xml.py\", line 48, in invoke_module\r\n    imp.load_module('__main__', mod, module, MOD_DESC)\r\n  File \"/tmp/ansible_xml_payload_RSmDlG/__main__.py\", line 868, in <module>\r\n  File \"/tmp/ansible_xml_payload_RSmDlG/__main__.py\", line 844, in main\r\n  File \"/tmp/ansible_xml_payload_RSmDlG/__main__.py\", line 408, in set_target_children\r\n  File \"/tmp/ansible_xml_payload_RSmDlG/__main__.py\", line 386, in set_target_children_inner\r\n  File \"/tmp/ansible_xml_payload_RSmDlG/__main__.py\", line 657, in children_to_nodes\r\n  File \"/tmp/ansible_xml_payload_RSmDlG/__main__.py\", line 645, in child_to_element\r\n  File \"lxml.etree.pyx\", line 921, in lxml.etree._Element.text.__set__ (src/lxml/lxml.etree.c:41344)\r\n  File \"apihelpers.pxi\", line 660, in lxml.etree._setNodeText (src/lxml/lxml.etree.c:18894)\r\n  File \"apihelpers.pxi\", line 1333, in lxml.etree._utf8 (src/lxml/lxml.etree.c:24601)\r\nTypeError: Argument must be bytes or unicode, got 'int'\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

如果我将剧本更改为以下内容,则效果很好:

---
- hosts: localhost
  gather_facts: no
  vars_files:
  - xml_vars.yml
  tasks:
  - name: modify xml file
    xml:
      path: /home/user/simple.xml
      xpath: /controller-info
      set_children: 
        - controller-host: "localhost"
        - controller-port: "443"
        - controller-ssl-enabled: "true"

我不知道为什么它不起作用。我确信这是我所忽略的简单事情。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我发现了问题。问题实际上是由controller_conf:行末尾的变量引起的。删除后,一切正常工作