ansible-collections / ansible.utils

A collection of ansible utilities for the content creator.
GNU General Public License v3.0
69 stars 70 forks source link

Failed to import the required Python library (netaddr) #352

Open TEDSv opened 1 month ago

TEDSv commented 1 month ago
SUMMARY

I have spent 2 days already and I cant fix that error. And I dont know what to do, please help :) When I run this task:

    - name: task netaddr
      debug:
        msg: "{{ '192.168.1.1' | ansible.utils.ipaddr('address') }}"

I get the error: Failed to import the required Python library (netaddr). I tried install also python3-netaddr, with become and with extra_args: "--user", but still same error.

ISSUE TYPE
COMPONENT NAME

ansible.utils.ipaddr

ANSIBLE VERSION
ansible [core 2.14.5]
  config file = None
  configured module search path = ['/home/semaphore/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.11/site-packages/ansible
  ansible collection location = /home/semaphore/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.8 (main, Feb 19 2024, 22:58:08) [GCC 12.2.1 20220924] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
ansible.netcommon             4.1.0
ansible.posix                 1.5.2
ansible.utils                 2.9.0
CONFIGURATION
~ $ ansible-config dump --only-changed
CONFIG_FILE() = None
HOST_KEY_CHECKING(env: ANSIBLE_HOST_KEY_CHECKING) = False
OS / ENVIRONMENT

target os - ubuntu 22.04, clean fresh virtual machine

STEPS TO REPRODUCE
- name: Playbook netaddr
  hosts: all
  vars:
    ansible_python_interpreter: /usr/bin/python3

  tasks:

    - name: Install packages
      apt:
        name:
          - python3
          - python3-pip
        state: present
      become: yes

    - name: install netaddr with pip3
      pip:
        name: netaddr
        state: present
        executable: pip3
        extra_args: "--user"
      vars:
        ansible_python_interpreter: /usr/bin/python3

    - name: check netaddr
      command: python3 -c "import netaddr; print(netaddr.__version__)"
      register: netaddr_version

    - name: show version netaddr
      debug:
        msg: "Netaddr version: {{ netaddr_version.stdout }}"

    - name: check python
      command: which python3
      register: python_path

    - name: show python
      debug:
        msg: "Python: {{ python_path.stdout }}"

    - name: Ensure modules are installed
      pip:
        name:         [ "netaddr"]
        extra_args:   "--user"
      vars:
        ansible_python_interpreter: "/usr/bin/python3"

    - name: task netaddr
      debug:
        msg: "{{ '192.168.1.1' | ansible.utils.ipaddr('address') }}"
EXPECTED RESULTS
ACTUAL RESULTS

"msg": "Failed to import the required Python library (netaddr) on e9498466e11b's Python /usr/bin/python3. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter" image

"msg": "Netaddr version: 1.2.1"
"msg": "Python: /usr/bin/python3"
TEDSv commented 1 month ago

forgot python version from destination vm this version installed ansible image

roverflow commented 1 month ago

@TEDSv could you initialize /usr/bin/python3 and try importing netaddr see if that works, it might be an env issue