gpt4 book ai didi

python - 如何使 junos_config Ansible 模块支持非 ASCII 字符?

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:56 26 4
gpt4 key购买 nike

我正在使用 junos_config Ansible 版本 2.2.1.0 中的模块,简而言之,它采用纯文本配置文件并使用它来配置 Juniper 网络设备。它似乎使用 lxml在这个过程中。问题是,这个纯文本文件包含一个非 ASCII 字符,Ansible 失败并出现以下错误:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
fatal: [10.10.10.111]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_FZ42iu/ansible_module_junos_config.py\", line 344, in <module>\n main()\n File \"/tmp/ansible_FZ42iu/ansible_module_junos_config.py\", line 335, in main\n run(module, result)\n File \"/tmp/ansible_FZ42iu/ansible_module_junos_config.py\", line 293, in run\n return load_config(module, result)\n File \"/tmp/ansible_FZ42iu/ansible_module_junos_config.py\", line 258, in load_config\n diff = module.config.load_config(candidate, **kwargs)\n File \"/tmp/ansible_FZ42iu/ansible_modlib.zip/ansible/module_utils/netcfg.py\", line 58, in load_config\n File \"/tmp/ansible_FZ42iu/ansible_modlib.zip/ansible/module_utils/junos.py\", line 201, in load_config\n File \"/usr/local/lib/python2.7/dist-packages/jnpr/junos/utils/config.py\", line 388, in load\n return try_load(rpc_contents, rpc_xattrs)\n File \"/usr/local/lib/python2.7/dist-packages/jnpr/junos/utils/config.py\", line 350, in try_load\n got = self.rpc.load_config(rpc_contents, **rpc_xattrs)\n File \"/usr/local/lib/python2.7/dist-packages/jnpr/junos/rpcmeta.py\", line 79, in load_config\n rpc.append(E('configuration-text', contents))\n File \"/usr/local/lib/python2.7/dist-packages/lxml/builder.py\", line 236, in __call__\n v = t(elem, item)\n File \"/usr/local/lib/python2.7/dist-packages/lxml/builder.py\", line 185, in add_text\n elem.text = (elem.text or \"\") + item\n File \"src/lxml/lxml.etree.pyx\", line 1031, in lxml.etree._Element.text.__set__ (src/lxml/lxml.etree.c:53225)\n File \"src/lxml/apihelpers.pxi\", line 715, in lxml.etree._setNodeText (src/lxml/lxml.etree.c:24420)\n File \"src/lxml/apihelpers.pxi\", line 703, in lxml.etree._createTextNode (src/lxml/lxml.etree.c:24283)\n File \"src/lxml/apihelpers.pxi\", line 1443, in lxml.etree._utf8 (src/lxml/lxml.etree.c:31502)\nValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters\n", "module_stdout": "", "msg": "MODULE FAILURE"}

我尝试在 junos_config 中使用 # -*- coding: utf-8 -*-模块,但这没有帮助。我还尝试将 Python PYTHONIOENCODING 设置为 utf8

junos_config 的配置文件模块如下所示:

groups {
replace: classes {
/* Configured by Ansible £ */
system {
login {
class test {
idle-timeout 1;
permissions network;
allow-commands "show route.*|quit|exit";
deny-commands .*;
}
}
}
}
}

当它包含 £ 字符时,模块将失败并显示 无法加载配置:所有字符串必须与 XML 兼容:Unicode 或 ASCII,没有 NULL 字节或控制字符错误信息。

如何制作junos_config (或 lxml )以支持非 ASCII 字符?

最佳答案

如果不检查源代码并且没有您的配置文件或有问题的字符串,我会说您的问题是非 ASCII 和非 UTF-8 字符。来自错误信息:

ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters

我会说它是一个控制字符。

经过一些研究,我找到了这个答案 https://stackoverflow.com/a/28380315/3810217也许有帮助。使用 hexdump 文件名,您可以检查空字节等。

另一种解决方案是使用 gedit 重新创建/保存文件。您可以在那里将所需的编码设置为 utf-8(至少对于新文件)。

关于python - 如何使 junos_config Ansible 模块支持非 ASCII 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45015187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com