Python 3: Unexpected Exception: name 'basestring' is not defined when invoking ansible2
When we do
ansible-playbook -vvv -i my/inventory my/playbook.yml
We may endup getting following error
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
The reason for above error is Ansible below version 2.5 requires Python 2.6 or 2.7 on the control host: Control Machine Requirements and
basestring is no longer available in Python 3. The documentation says
basestringabstract type was removed. Use
bytestypes don’t have functionality enough in common to warrant a shared base class. The
2to3tool (see below) replaces every occurrence of
If we install ansible using
pip3, it erroneously pointsto python3, but if you do
pip2 install ansible, it will work fine, putting this at the top of each ansible utility:
Another workaround may be using the following code at top of the script.
try: basestring except NameError: basestring = str
We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.