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

PythonPython 3.xAnsibleAnsible 2.x

Python Problem Overview


I'm trying to execute 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 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 2.0.0.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

And here is python --version

Python 3.4.3

Python Solutions


Solution 1 - Python

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

basestring is no longer available in Python 3. From What’s New In Python 3.0:

> The builtin basestring abstract type was removed. Use str instead. The str and bytes types don’t have functionality enough in common to warrant a shared base class. The 2to3 tool (see below) replaces every occurrence of basestring with str.

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

Solution 2 - Python

basestring is not available in Python 3.:

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

try:
  basestring
except NameError:
  basestring = str

Solution 3 - Python

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:

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

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

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

Solution 4 - Python

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

Solution 5 - Python

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

Solution 6 - Python

Another possible solution is to install future via pip install future and import from past.builtins import basestring.

from past.builtins import basestring

Personally, I'm not crazy about this solution because:

  1. It requires installing another dependency.
  2. There are likely other backward compatibility issues mixing Python 3 and 2 code.
  3. It will only work in your code - you don't want to modify Ansible source code.

I offer it only because it's something I've used in the past to make my own code work in both Python 2 and 3.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncode_monkView Question on Stackoverflow
Solution 1 - PythonudondanView Answer on Stackoverflow
Solution 2 - PythonChris LaidlerView Answer on Stackoverflow
Solution 3 - PythoneodgoochView Answer on Stackoverflow
Solution 4 - PythonDila GurungView Answer on Stackoverflow
Solution 5 - PythonDila GurungView Answer on Stackoverflow
Solution 6 - PythoncycgrogView Answer on Stackoverflow