Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TEDSv opened this issue May 23, 2024 · 3 comments
Open

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

TEDSv opened this issue May 23, 2024 · 3 comments
Labels
needs_info This issue requires further information. Please answer any outstanding questions.

Comments

@TEDSv
Copy link

TEDSv commented May 23, 2024

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
  • Bug Report
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
Copy link
Author

TEDSv commented May 23, 2024

forgot python version from destination vm
this version installed ansible
image

@roverflow
Copy link
Member

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

@KB-perByte KB-perByte added the needs_info This issue requires further information. Please answer any outstanding questions. label Jun 5, 2024
@cgbarthel
Copy link

Might be the library is just not installed in a location Ansible can find it. I use pipx to install python modules, and had to do this (not sure the proper way, but this worked for me):

cp -R /home//.local/lib/python3.9/site-packages/netaddr* /home//.local/pipx/venvs/ansible/lib/pythone3.9/site-packages/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs_info This issue requires further information. Please answer any outstanding questions.
Projects
None yet
Development

No branches or pull requests

4 participants