Facing issues when using ansible 2.16 with python 3.11

I am facing below issue when gathering facts with ansible 2.16. As we are upgrading our environment from ansible 2.14 to ansible 2.16.

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => changed=false
  ansible_facts: {}
  failed_modules:
    ansible.legacy.setup:
      exception: |-
        Traceback (most recent call last):
          File "/root/.ansible/tmp/ansible-tmp-1770954071.9596925-56009-84847984617131/AnsiballZ_setup.py", line 107, in <module>
            _ansiballz_main()
          File "/root/.ansible/tmp/ansible-tmp-1770954071.9596925-56009-84847984617131/AnsiballZ_setup.py", line 99, in _ansiballz_main
            invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
          File "/root/.ansible/tmp/ansible-tmp-1770954071.9596925-56009-84847984617131/AnsiballZ_setup.py", line 47, in invoke_module
            runpy.run_module(mod_name='ansible.modules.ansible.legacy.setup', init_globals=dict(_module_fqn='ansible.modules.ansible.legacy.setup', _modlib_path=modlib_path),
          File "<frozen runpy>", line 226, in run_module
          File "<frozen runpy>", line 98, in _run_module_code
          File "<frozen runpy>", line 88, in _run_code
          File "/tmp/ansible_ansible.legacy.setup_payload_xfjj0shm/ansible_ansible.legacy.setup_payload.zip/ansible/modules/ansible/legacy/setup.py", line 175, in <module>
        ModuleNotFoundError: No module named 'ansible.modules.ansible.module_utils'
      failed: true
      module_stderr: |-
        Traceback (most recent call last):
          File "/root/.ansible/tmp/ansible-tmp-1770954071.9596925-56009-84847984617131/AnsiballZ_setup.py", line 107, in <module>
            _ansiballz_main()
          File "/root/.ansible/tmp/ansible-tmp-1770954071.9596925-56009-84847984617131/AnsiballZ_setup.py", line 99, in _ansiballz_main
            invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
          File "/root/.ansible/tmp/ansible-tmp-1770954071.9596925-56009-84847984617131/AnsiballZ_setup.py", line 47, in invoke_module
            runpy.run_module(mod_name='ansible.modules.ansible.legacy.setup', init_globals=dict(_module_fqn='ansible.modules.ansible.legacy.setup', _modlib_path=modlib_path),
          File "<frozen runpy>", line 226, in run_module
          File "<frozen runpy>", line 98, in _run_module_code
          File "<frozen runpy>", line 88, in _run_code
          File "/tmp/ansible_ansible.legacy.setup_payload_xfjj0shm/ansible_ansible.legacy.setup_payload.zip/ansible/modules/ansible/legacy/setup.py", line 175, in <module>
        ModuleNotFoundError: No module named 'ansible.modules.ansible.module_utils'
      module_stdout: ''
      msg: |-
        MODULE FAILURE
        See stdout/stderr for the exact error
      rc: 1
  msg: |-
    The following modules failed to execute: ansible.legacy.setup```

I am using virtual env. Let me know incase you need more information

Do you have a setup module in any of the module paths or in a library/ directory alongside your playbook? You can check the configured module paths with ansible-config dump | grep DEFAULT_MODULE_PATH. I can reproduce this by copying the builtin setup module into my library/, but it doesn’t work on 2.14 either. Changing the relative import (from ..module_utils.basic import AnsibleModule) to an absolute import (from ansible.module_utils.basic import AnsibleModule) fixes it.

1 Like

We are using pyenv for ansible 2.16 and here is the cfg file:

(pyenv-ansible-2.16) [dev] root@foreman:pyenv-ansible-2.16# cat ../ansible-2.16-base.cfg
[defaults]
stdout_callback = yaml
library = pyenv-ansible-2.16/lib/python3.11/site-packages/ansible/modules:ansible-module-foreman:./extra/ceph-ansible/library
action_plugins = plugins/actions:./extra/ceph-ansible/plugins/actions
callback_plugins = plugins/callback
filter_plugins = plugins/filter:./filter_plugins
lookup_plugins = lookup_plugins
roles_path = ./extra/kubespray/roles/:./roles:./extra/ceph-ansible/roles

If I remove this path pyenv-ansible-2.16/lib/python3.11/site-packages/ansible/modules then I didn’t see this issue. Also this folder contain below files:

(pyenv-ansible-2.16) [dev] root@foreman:modules# ls | xargs
add_host.py apt_key.py apt.py apt_repository.py assemble.py assert.py async_status.py async_wrapper.py blockinfile.py command.py copy.py cron.py deb822_repository.py debconf.py debug.py dnf5.py dnf.py dpkg_selections.py expect.py fail.py fetch.py file.py find.py gather_facts.py getent.py get_url.py git.py group_by.py group.py hashivault hostname.py import_playbook.py import_role.py import_tasks.py include_role.py include_tasks.py include_vars.py __init__.py iptables.py known_hosts.py lineinfile.py meta.py package_facts.py package.py pause.py ping.py pip.py __pycache__ raw.py reboot.py replace.py rpm_key.py script.py service_facts.py service.py set_fact.py set_stats.py setup.py shell.py slurp.py stat.py subversion.py systemd.py systemd_service.py sysvinit.py tempfile.py template.py unarchive.py uri.py user.py validate_argument_spec.py wait_for_connection.py wait_for.py yum.py yum_repository.py

The ansible.builtin paths don’t need to be configured, so removing it makes sense. At best it does nothing.

The error occurs if the setup.py in your library is not the same as the ansible.builtin.setup module (and is executed as ansible.legacy.setup, like gather_facts does by default). The relative import works for the package ansible.modules.setup, but you can see in your traceback that the package is ansible.modules.ansible.legacy.setup (which would require a relative import like from ....module_utils.basic import AnsibleModule or absolute import instead).