Python 意外异常:调用 ansible2 时未定义名称“basestring”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34803467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:32:45  来源:igfitidea点击:

Unexpected Exception: name 'basestring' is not defined when invoking ansible2

pythonpython-3.xansibleansible-2.x

提问by code_monk

I'm trying to execute ansible2 commnads...

我正在尝试执行 ansible2 commnads ...

When I do:

当我做:

ansible-playbook -vvv -i my/inventory my/playbook.yml

I get:

我得到:

Unexpected Exception: name 'basestring' is not defined the full traceback was:

Traceback (most recent call last):
  File "/usr/local/bin/ansible-playbook", line 85, in <module>
    sys.exit(cli.run())
  File "/usr/local/lib/python3.4/site-packages/ansible/cli/playbook.py", line 150, in run
    results = pbex.run()
  File "/usr/local/lib/python3.4/site-packages/ansible/executor/playbook_executor.py", line 87, in run
    self._tqm.load_callbacks()
  File "/usr/local/lib/python3.4/site-packages/ansible/executor/task_queue_manager.py", line 149, in load_callbacks
    elif isinstance(self._stdout_callback, basestring):
NameError: name 'basestring' is not defined

意外异常:名称“basestring”未定义完整回溯为:

Traceback (most recent call last):
  File "/usr/local/bin/ansible-playbook", line 85, in <module>
    sys.exit(cli.run())
  File "/usr/local/lib/python3.4/site-packages/ansible/cli/playbook.py", line 150, in run
    results = pbex.run()
  File "/usr/local/lib/python3.4/site-packages/ansible/executor/playbook_executor.py", line 87, in run
    self._tqm.load_callbacks()
  File "/usr/local/lib/python3.4/site-packages/ansible/executor/task_queue_manager.py", line 149, in load_callbacks
    elif isinstance(self._stdout_callback, basestring):
NameError: name 'basestring' is not defined

Here is ansible --version:

这是ansible --version

ansible 2.0.0.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

And here is python --version

这里是 python --version

Python 3.4.3

采纳答案by udondan

Ansible below version 2.5 requires Python 2.6 or 2.7 on the control host: Control Node Requirements

Ansible 2.5 以下版本需要控制主机上的 Python 2.6 或 2.7:控制节点要求

basestringis no longer available in Python 3. From What's New In Python 3.0:

basestring在 Python 3 中不再可用。来自Python 3.0 的新增功能

The builtin basestringabstract type was removed. Use strinstead. The strand bytestypes don't have functionality enough in common to warrant a shared base class. The 2to3tool (see below) replaces every occurrence of basestringwith str.

basestring删除了内置抽象类型。使用str来代替。该strbytes类型没有足够的功能共同保证一个共享的基类。该2to3工具(见下文)取代的每次出现basestringstr

So the solution is to either upgrade Ansible or downgrade Python.

所以解决方案是升级 Ansible 或降级 Python。

回答by Chris Laidler

basestring is not available in Python 3.:

basestring 在 Python 3 中不可用。:

This can be fixed for python 2.x and 3.x with the following:

这可以通过以下方式为 python 2.x 和 3.x 修复:

try:
  basestring
except NameError:
  basestring = str

回答by eodgooch

I ran into this issue using Python 3 with Ansible and solved by forking the dopy project and installing dopy in my ansible script with:

我使用带有 Ansible 的 Python 3 遇到了这个问题,并通过 fork dopy 项目并在我的 ansible 脚本中安装 dopy 解决了这个问题:

name: git+https://github.com/eodgooch/[email protected]#egg=dopy.

name: git+https://github.com/eodgooch/[email protected]#egg=dopy.

If you are still getting errors be sure to change the versionto 0.4.0and remove any lingering dopy packages from your Python site-packages directory.

如果您仍然遇到错误,请确保更改version0.4.0并从您的 Python 站点包目录中删除任何挥之不去的 dopy 包。

Also you could pip3 install git+https://github.com/eodgooch/[email protected]#egg=dopyinstead of in your Ansible Task.

你也可以pip3 install git+https://github.com/eodgooch/[email protected]#egg=dopy代替你的 Ansible 任务。

回答by Dila Gurung

Replace basestring with str. In 2.x basestring is there. but in 3.x the basestring has been replaced with "str".

用 str 替换 basestring。在 2.x basestring 中有。但在 3.x 中,basestring 已被替换为“str”。

回答by Dila Gurung

The problem might be due to python version. In 2.x, basestring is there but in 3.x it has been replaced with "str".

问题可能是由于python版本。在 2.x 中,basestring 在那里,但在 3.x 中它已被替换为“str”。