Vignesh Paramasivam
Vignesh Paramasivam

Reputation: 2470

Ansible - "NameError: name 'urllib2' is not defined"

Getting this below error while trying to run ansible(version >2) with python 3.5.2

I have looked into the github issues terming it as resolved, but can't sort out what needs to be done. https://github.com/ansible/ansible/issues/16013

How to resolve this?

virtual@xxxxxxxxxx:~/ansible-spike> ansible all -m ping -vvv
    Using /home/virtual/ansible-spike/ansible.cfg as config file
    ERROR! Unexpected Exception: name 'urllib2' is not defined
    the full traceback was:

    Traceback (most recent call last):
      File "/home/virtual/.pyenv/versions/3.5.2/bin/ansible", line 92, in <module>
        exit_code = cli.run()
      File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/cli/adhoc.py", line 193, in run
        result = self._tqm.run(play)
      File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/executor/task_queue_manager.py", line 202, in run
        self.load_callbacks()
      File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/executor/task_queue_manager.py", line 171, in load_callbacks
        for callback_plugin in callback_loader.all(class_only=True):
      File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/plugins/__init__.py", line 368, in all
        self._module_cache[path] = self._load_module_source(name, path)
      File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/plugins/__init__.py", line 319, in _load_module_source
        module = imp.load_source(name, path, module_file)
      File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/imp.py", line 172, in load_source
        module = _load(spec)
      File "<frozen importlib._bootstrap>", line 693, in _load
      File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 665, in exec_module
      File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
      File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/plugins/callback/hipchat.py", line 32, in <module>
        from ansible.module_utils.urls import open_url
      File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/module_utils/urls.py", line 330, in <module>
        if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib2, 'HTTPSHandler'):
    NameError: name 'urllib2' is not defined

Upvotes: 0

Views: 4438

Answers (4)

Amir Md Amiruzzaman
Amir Md Amiruzzaman

Reputation: 2079

If you are using

import urllib2

Instead, you can call the library as

import urllib.request

Then use it like

urllib.request.urlopen('your url goes here')

This should get rid of the error you are getting

Upvotes: 0

mhawke
mhawke

Reputation: 87114

The ansible python API does not support Python 3. The PyPI page lists only 2.6 and 2.7 .

Upvotes: 1

Henrik Pingel
Henrik Pingel

Reputation: 3193

Ansible is currently not able to run with Python3. That is also stated in the linked Github Issue.

Upvotes: 0

Jeroen Jacobs
Jeroen Jacobs

Reputation: 1525

Urllib2 is specific to Python v2.

Urllib2 documentation at http://docs.python.org/library/urllib2.html:

The urllib2 module has been split across several modules in Python 3.0 named urllib.request and urllib.error.

I don't think Ansible is compatible with Python 3 yet.

Upvotes: 6

Related Questions