Unexpected Exception: name 'basestring' is not defined when invoking ansible2
Solution 1
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. Usestr
instead. Thestr
andbytes
types don’t have functionality enough in common to warrant a shared base class. The2to3
tool (see below) replaces every occurrence ofbasestring
withstr
.
So the solution is to either upgrade Ansible or downgrade Python.
Solution 2
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
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
Replace basestring with str. In 2.x basestring is there. but in 3.x the basestring has been replaced with "str".
Comments
-
code_monk over 4 years
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 <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 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
-
code_monk over 8 yearsin case this helps anyone else, when i installed ansible using
pip
andpip3
, it erroneously pointed to python3, but when i didpip2 install ansible
, it worked fine, putting this at the top of each ansible utility:#!/usr/local/opt/python/bin/python2.7
-
vastlysuperiorman about 8 yearsI feel like this is a flaw in pip or python packages or something. I shouldn't be able to pip3 install something that isn't compatible with python3.
-
wesgarrison almost 5 years
pip uninstall dopy
andpip3 install git+https://github.com/eodgooch/[email protected]#egg=dopy
worked for me. Thank you! -
absynce almost 5 yearsI was able to get this working by installing [email protected]:
python3 -m pip install ansible==2.5.0
. -
ostrokach almost 5 yearsNot that
isinstance(b"", basestring) == True
on Python 2 butisinstance(b"", str) == False
on Python 3. -
davidmpaz almost 4 yearsI like more this one since i need the code to run in both environments. I find it hacky but is the fit for me now. Thanks!
-
Serk over 3 yearsfollowing the documentation, why not use
(str, bytes)
asbasestring
? Forisinstance(var, (str,bytes))
works perfectly. -
udondan over 3 yearsSo your suggestion is to rewrite the ansible code? Sure, go ahead...