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 (hcloud-python) #139

Closed
jobetinfosec opened this issue Aug 1, 2021 · 11 comments
Closed

Failed to import the required Python library (hcloud-python) #139

jobetinfosec opened this issue Aug 1, 2021 · 11 comments
Labels

Comments

@jobetinfosec
Copy link

Bug Report

Current Behavior

When running an Ansible script, an error shows up and the script stops:
"Failed to import the required Python library (hcloud-python) on computer's Python /usr/bin/python3. Please read the module documentation and install it in the appropriate location."

Input Code

- name: Create a basic Hetzner hcloud server with ssh key
  hcloud_server:
    api_token: "{{ hcloud_token }}"
    image: "{{ hcloud_image }}"
    location:  "{{ hcloud_location }}"
    name: "{{ item }}"
    server_type: "{{ hcloud_type }}"
    ssh_keys:
      - "{{ hcloud_ssh }}"
    state: present
  with_inventory_hostnames:
    - webservers
  register: hcloud_info

Expected behavior/code

Virtual server creation

Environment

  • Linux Ubuntu Desktop [21.04]
  • Python Version: [3.9.5]
  • Hcloud-Python Version: [1.13.0]
  • Ansible version: [2.11.3]
  • Ansible Galaxy hcloud collection version: [1.4.4]
@resmo
Copy link

resmo commented Aug 24, 2021

not a bug

I assume you installed ansible with python2 pip. But hcloud is python3 only. You should consider to use python3 venv anyway to be independend of systems python and OS.

apt-get install python3 python3-pip python3-venv
cd to-your-playbooks
python3 -m venv venv
source venv/bin/activate

# you are now in a venv, install python deps
pip install ansible hcloud-python

# to get your PATH updated, leave venv with deactivate and activate again
deactivate
source venv/bin/activate

ansible --version

@jobetinfosec
Copy link
Author

jobetinfosec commented Nov 14, 2021

Hi resmo,
I didn't install Ansible with pip 2, but directly with Ubuntu's apt manager. I installed hcloud with pip3 (pip3 install hcloud).
I tried once again with 2 differents Ubuntu versions, 20.04 (with Python 3.8) and 21.10 (Python 3.9) but both are throwing the same error: Failed to import the required Python library (hcloud-python) on computer's Python /usr/bin/python3

The only laptop where this is working is on a MacBook Pro (Python 3.9 and hcloud 1.12).
Comparing a bit both installations (Mac and Ubuntu) I noticed something that maybe could be relevant...
running the command which hcloud shows /usr/local/bin/hcloud (in Mac laptop) but nothing shows up in the Ubuntu laptop....

@resmo
Copy link

resmo commented Nov 15, 2021

hi @jobetinfosec

Debian packages for ansible used to rely on python2 and that is why it will not use python3 libraries. Install ansible in a venv for python3 as I posted.

@jobetinfosec
Copy link
Author

jobetinfosec commented Nov 16, 2021

Bonjour @resmo

Now the problem with hcloud is gone, but now a SSH key not found error shows up... ;-)
In my Ansible's hosts file I have this directive: ansible_ssh_private_key_file=~/.ssh/id_rsa

When running Ansible in a virtual environment, does it lookup for files outside the virtual env? or is limited only to it?

@resmo
Copy link

resmo commented Nov 17, 2021

@jobetinfosec lookup is not limited to the venv, the venv is only for python dependency it is not a "container" or a "secured environment"

Nevertheless, I tested it and confirm this worked.

- hosts: localhost
  tasks:
  - debug:
      msg: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

@psi-4ward
Copy link

psi-4ward commented Dec 21, 2021

Same here on arch-linux

Ansible reports Python /usr/bin/python

$ /usr/bin/python --version
Python 3.9.7

$ pip --version
pip 20.3.4 from /usr/lib/python3.9/site-packages/pip (python 3.9)

$ pip install hcloud-python
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement hcloud-python
ERROR: No matching distribution found for hcloud-python

$ pip install hcloud
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: hcloud in /home/user/.local/lib/python3.9/site-packages (1.16.0)

SOLVED:
The playbook uses become (root) and the hcloud-pkg was installed for the user.
So become: false in the task solves it. :)

@olegsidokhmetov
Copy link

olegsidokhmetov commented Mar 24, 2022

I have the same issue ansible-playbook -vvv server-create-init.yml

my playbook

---

- hosts: localhost 
  gather_facts: false
  become: false 
  vars_files:
    - /etc/ansible/roles/server-create/defaults/main.yml
    - /etc/ansible/roles/server-create/vars/main.yml

  roles:
    - role: "/etc/ansible/roles/server-create"

Task:

---

- name: Create a basic server
  hcloud_server:
    api_token: "{{ token }}"
    name: "{{ item.name }}"
    server_type: "{{ item.server_type }}"
    image: "{{ item.os_image }}"
    state: present
  register: server
  with_items: "{{ server }}"

Can not find any solves of this issue (hcloud-python) . Could you please help with solution?

root@ip-172-31-27-151:/usr/bin# pip install ansible hcloud-python

Requirement already satisfied: ansible in /usr/lib/python3/dist-packages (5.4.0)
ERROR: Could not find a version that satisfies the requirement hcloud-python (from versions: none)
ERROR: No matching distribution found for hcloud-python

root@ip-172-31-27-151:/usr/bin# python3 --version

Python 3.8.10

/etc/ansible/hosts

[Server_hetzner]
localhost ansible_connection=local

[all:vars]
ansible_python_interpreter=/usr/bin/python3

root@ip-172-31-27-151:/usr/bin# cat /etc/os-release

NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

@RogelioCodes
Copy link

I have the same issue ansible-playbook -vvv server-create-init.yml

my playbook

---

- hosts: localhost 
  gather_facts: false
  become: false 
  vars_files:
    - /etc/ansible/roles/server-create/defaults/main.yml
    - /etc/ansible/roles/server-create/vars/main.yml

  roles:
    - role: "/etc/ansible/roles/server-create"

Task:

---

- name: Create a basic server
  hcloud_server:
    api_token: "{{ token }}"
    name: "{{ item.name }}"
    server_type: "{{ item.server_type }}"
    image: "{{ item.os_image }}"
    state: present
  register: server
  with_items: "{{ server }}"

Can not find any solves of this issue (hcloud-python) . Could you please help with solution?

root@ip-172-31-27-151:/usr/bin# pip install ansible hcloud-python

Requirement already satisfied: ansible in /usr/lib/python3/dist-packages (5.4.0)
ERROR: Could not find a version that satisfies the requirement hcloud-python (from versions: none)
ERROR: No matching distribution found for hcloud-python

root@ip-172-31-27-151:/usr/bin# python3 --version

Python 3.8.10

/etc/ansible/hosts

[Server_hetzner]
localhost ansible_connection=local

[all:vars]
ansible_python_interpreter=/usr/bin/python3

root@ip-172-31-27-151:/usr/bin# cat /etc/os-release

NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

I just wanted to check in and say the same thing started happening to me about 4 days ago as well. I was out of office for the weekend and only noticed today(monday)

@github-actions
Copy link

This issue has been marked as stale because it has not had recent activity. The bot will close the issue if no further action occurs.

@jimkoen
Copy link

jimkoen commented Sep 12, 2022

Having the same issue here, on Mac OS Monterey with Python/Pip 3.10

EDIT: For anyone encountering this error, while the error message calls the package hcloud-python, when installing it via pip, you do in fact want just want hcloud

pip install hcloud

@cx-networks-gmbh
Copy link

Maybe this can help someone in the future:
We had the same issue on ubuntu 22.04. We were moving from an installation without virtual env to an installation with virtual env. The tasks originally executed on an "ansible" group:

# hosts.yml
---
all:
  hosts:
    ansible:
      ansible_host: localhost
      ansible_connection: local
# TestPlaybook.yml
---
- name: Get some infos
  hosts: ansible
  gather_facts: false
  tasks:
    - name: Gather facts about the cloud # <-- Fails
      hetzner.hcloud.hcloud_server_info:
        name: "testhost"
        api_token: "{{ hetzner_cloud_api_token }}"

Therefore ansible set the python3 interpreter to usr/bin/python3 but the python interpreter was in the virtual enviroment ~/venv/bin/python3. Setting the hosts to just localhost fixed it for us.

# TestPlaybook.yml
---
- name: Get some infos
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Gather facts about the cloud # <-- Fails
      hetzner.hcloud.hcloud_server_info:
        name: "testhost"
        api_token: "{{ hetzner_cloud_api_token }}"

Were are wondering about why the python interpreter in the venv does import the library without errors because it is just a symbolic link to /usr/bin/python3 but we did not investigate it further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants