Getting 'open_url' cannot import error

I have a python (v3.12.x) installation of Ansible, ansible-core, ansible-galaxy, etc. and I am unable to make any calls to ‘open_url’ or ‘get_url’ either directly or indirectly through another task that may call these two utilities.

ansible [core 2.17.6]
config file = /home/greggl/.ansible.cfg
configured module search path = [‘/home/greggl/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /home/greggl/.pyenv/versions/3.12.4/lib/python3.12/site-packages/ansible
ansible collection location = /home/greggl/.ansible/collections/ansible_collections
executable location = /home/greggl/.pyenv/versions/3.12.4/bin/ansible
python version = 3.12.4 (main, Oct 15 2024, 15:27:28) [GCC 13.2.0] (/home/greggl/.pyenv/versions/3.12.4/bin/python3)
jinja version = 3.1.4
libyaml = True

You have to show your ansible code and the errors you receive, plus results of basic troubleshooting (example - trying these commands from the command line without ansible), or else no one will be able to assist you…

OK. My error is not stemming from any thing I am coding, other than calling our a task that makes use of ‘get_url’ or ‘open_url’. This same error crops up when I can a ‘yum’ task, a ‘dnf’ task, a ‘get_url’ direct task, ANY task that reaches out to the ‘url’ Python module.

Here is my one Ansible task that draws the error:

  • name: Install Prerequisite Packages
    ansible.builtin.package:
    name:
    - curl
    - wget
    - ca-certificates
    - nfs-utils
    - python3-pip
    - postgresql-contrib
    - postgresql
    - lvm2
    state: present
    environment: “{{ proxy_env }}”

And the resulting error is:

TASK [infra.prerequisites.install : Install Prerequisite Packages] ***********************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: cannot import name ‘open_url’ from partially initialized module ‘ansible.module_utils.urls’ (most likely due to a circular import) (/tmp/ansible_ansible.legacy.dnf_payload_ak5cc1qa/ansible_ansible.legacy.dnf_payload.zip/ansible/module_utils/urls.py)
fatal: [x.x.x.x]: FAILED! => {“changed”: false, “module_stderr”: “Shared connection to [x.x.x.x] closed.\r\n”, “module_stdout”: “Traceback (most recent call last):\r\n File "/var/home/ec2-user/.ansible/tmp/ansible-tmp-1731341024.3789487-31629-124459087336013/AnsiballZ_dnf.py", line 107, in \r\n _ansiballz_main()\r\n File "/var/home/ec2-user/.ansible/tmp/ansible-tmp-1731341024.3789487-31629-124459087336013/AnsiballZ_dnf.py", line 99, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File "/var/home/ec2-user/.ansible/tmp/ansible-tmp-1731341024.3789487-31629-124459087336013/AnsiballZ_dnf.py", line 47, in invoke_module\r\n runpy.run_module(mod_name=‘ansible.modules.dnf’, init_globals=dict(_module_fqn=‘ansible.modules.dnf’, _modlib_path=modlib_path),\r\n File "", line 226, in run_module\r\n File "", line 98, in _run_module_code\r\n File "", line 88, in _run_code\r\n File "/tmp/ansible_ansible.legacy.dnf_payload_ak5cc1qa/ansible_ansible.legacy.dnf_payload.zip/ansible/modules/dnf.py", line 404, in \r\n File "/tmp/ansible_ansible.legacy.dnf_payload_ak5cc1qa/ansible_ansible.legacy.dnf_payload.zip/ansible/module_utils/urls.py", line 18, in \r\nImportError: cannot import name ‘open_url’ from partially initialized module ‘ansible.module_utils.urls’ (most likely due to a circular import) (/tmp/ansible_ansible.legacy.dnf_payload_ak5cc1qa/ansible_ansible.legacy.dnf_payload.zip/ansible/module_utils/urls.py)\r\n”, “msg”: “MODULE FAILURE\nSee stdout/stderr for the exact error”, “rc”: 1}

It “seems” to be a generic error related to Python installed Ansible versus RHEL RPM ansible installation.

I’ve searched over and over via Google, and not one direct article relates to this issue I am seeing.

Anyone have any clues on this? I am using Ubuntu 24.04 with Python3.12 installed Ansible.

One thing that I have seen to fix this issue is to apt-get install ansible INSTEAD of python install ansible. For some reason, the python-installed Ansible is hitting this error.

By “Python installed Ansible” do you mean Ansible was installed using pip3 or pipx or something else?

If you have Ansible installed in your users $HOME directory how are you running it to ensure that this version is used rather a system packaged version of Ansible, eg is it in ~/.local/bin and does that directory come before others in your $PATH?

$ pyenv local 3.12.4
$ which python3
/home/greggl/.pyenv/shims/python3
$ which ansible
/home/greggl/.pyenv/shims/ansible
$ ansible --version
ansible [core 2.17.6]
config file = /home/greggl/.ansible.cfg
configured module search path = [‘/home/greggl/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /home/greggl/.pyenv/versions/3.12.4/lib/python3.12/site-packages/ansible
ansible collection location = /home/greggl/.ansible/collections/ansible_collections
executable location = /home/greggl/.pyenv/versions/3.12.4/bin/ansible
python version = 3.12.4 (main, Oct 15 2024, 15:27:28) [GCC 13.2.0] (/home/greggl/.pyenv/versions/3.12.4/bin/python3)
jinja version = 3.1.4
libyaml = True

Could this error be from one of those paths in my ansible.cfg?

That all looks fine, perhaps uninstall the system Ansible to be sure that the issue isn’t caused by different versions conflicting? However this isn’t an issue I’ve had — all the Ansible controllers I use have a Debian Ansible and a pipx installed Ansible in my $HOME… (I use the Debian Ansible to install the latest Ansible using pipx).

It turns out this Ubuntu image doesn’t have system ansible installed. I think reverting back to RPM-based Ansible installations is what fixes this issue, if I can remember correctly.

I prefer the pip3 installation of Ansible over the system installed Ansible, but I can’t seem to get around using tasks that ultimately hit the get_url or open_url.

Very frustrating. And apparently no one else on the Internets has seen this issue before.

So I bit the bullet and apt installed Ansible-core and Ansible, to go along with my pip3 installed Ansible and ran my playbook and got the same “cannot import name ‘open_url’ from partially initialized modules 'ansible.module_utils.urls”

I am starting to believe this is just a pathing issue of some sort. The module is there since ansible-core brings along all of the ansible.builtin.* tasks, but which pathing is limiting this from being found?

I’m sitting in $HOME/.ansible and can find the urls.py module at ./plugins/module_utils/urls.py. What do I need to set/change to tell Ansible to look here too?