首页 > 解决方案 > ImportError:没有名为 influxdb 的模块。无法导入所需的 Python 库 (influxdb)

问题描述

我在使用“influxdb_database”模块通过 ansible 管理 influxdb 时遇到问题。即使它打印出有关 python 依赖的错误,但只有当运行 ansible playbook 的容器在来自 influxdb 托管的 diff VM 上运行时,它才会失败。我从 docker 容器运行 ansible playbook,如果我在安装了 influxdb 的主机上运行容器,它工作正常 - 它正在管理创建数据库。但是,当相同的容器(从与前面提到的相同的映像创建)在与 influxdb 托管的虚拟机不同的虚拟机上运行时,它会失败,并在下面粘贴错误。所以我现在对关于 python 依赖的错误感到困惑,不明白问题出在哪里。

Ansible 剧本:

hosts: "tag_deployment_sysmiromis:&tag_service_tick_yes"
  user: centos
  become: yes
 
  tasks:
- name: Install InfluxDB package
  yum: name="influxdb-{{ frame_tick_influxdb_package_version }}" state=present disable_gpg_check=yes
  register: frame_yum_run
  retries: 10
  until: frame_yum_run is success

- name: Restrict InfluxDB user login
  user:
    name: "influxdb"
    group: "influxdb"
    shell: /sbin/nologin

- name: Enable InfluxDB service
  systemd:
    name: influxdb
    enabled: yes
    state: started

- name: Create InfluxDB data directory
  file:
    path: "{{ frame_tick_influxdb_data_directory }}"
    owner: influxdb
    group: influxdb
    state: directory
    mode: 0750

- name: Create database
  influxdb_database:
      hostname: localhost
      database_name: miroslav

Ansible 登录失败的任务

TASK [Create database] ***********************************************************************************************************************************************************
task path: /app/lib/ansible/playbooks/influx.yml:6
Using module file /usr/lib/python3.8/site-packages/ansible/modules/database/influxdb/influxdb_database.py
Pipelining is enabled.
<10.246.44.196> ESTABLISH SSH CONNECTION FOR USER: centos
<10.246.44.196> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="centos"' -o ConnectTimeout=30 -o ControlPath=/root/.ansible/cp/be4c96d801 10.246.44.196 '/bin/sh -c '"'"'sudo -H -S -n  -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-brzvkupumuacfsjirccgazqszuzzfwwx ; /usr/bin/python'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded
<10.246.44.196> (1, b'\n{"msg": "Failed to import the required Python library (influxdb) on frame-tick10-246-44-196\'s Python /usr/bin/python. Please read module documentation and install 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", "failed": true, "exception": "Traceback (most recent call last):\\n  File \\"/tmp/ansible_influxdb_database_payload_IrdxhN/ansible_influxdb_database_payload.zip/ansible/module_utils/influxdb.py\\", line 23, in <module>\\n    from influxdb import InfluxDBClient\\nImportError: No module named influxdb\\n", "invocation": {"module_args": {"username": "root", "retries": 3, "use_udp": true, "proxies": {}, "database_name": "miroslav", "hostname": "localhost", "udp_port": 4444, "ssl": false, "state": "present", "timeout": null, "password": "root", "validate_certs": true, "port": 8086}}}\n', b'OpenSSH_8.1p1, OpenSSL 1.1.1g  21 Apr 2020\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug2: resolve_canonicalize: hostname 10.246.44.196 is address\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 2147\r\ndebug3: mux_client_request_session: session request sent\r\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\n')
<10.246.44.196> Failed to connect to the host via ssh: OpenSSH_8.1p1, OpenSSL 1.1.1g  21 Apr 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolve_canonicalize: hostname 10.246.44.196 is address
debug1: auto-mux: Trying existing master
debug2: fd 3 setting O_NONBLOCK
debug2: mux_client_hello_exchange: master version 4
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote
debug3: mux_client_request_session: entering
debug3: mux_client_request_alive: entering
debug3: mux_client_request_alive: done pid = 2147
debug3: mux_client_request_session: session request sent
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 1
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_influxdb_database_payload_IrdxhN/ansible_influxdb_database_payload.zip/ansible/module_utils/influxdb.py", line 23, in <module>
    from influxdb import InfluxDBClient
ImportError: No module named influxdb
fatal: [10.246.44.196]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "database_name": "miroslav",
            "hostname": "localhost",
            "password": "root",
            "port": 8086,
            "proxies": {},
            "retries": 3,
            "ssl": false,
            "state": "present",
            "timeout": null,
            "udp_port": 4444,
            "use_udp": true,
            "username": "root",
            "validate_certs": true
        }
    }
}

MSG:

Failed to import the required Python library (influxdb) on frame-tick10-246-44-196's Python /usr/bin/python. Please read module documentation and install 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

标签: python-3.xansibleinfluxdb

解决方案


听起来你和我处理同样的问题。我正在努力找出问题所在,然后我阅读了要求并看到了这一点。我正在使用比这个模块支持的更新版本的 influxdb,所以我得到了和你一样的错误

要求 执行此模块的主机需要以下要求。

蟒蛇> = 2.6

涌入数据库 >= 0.9 & <= 1.2.4

要求

https://docs.ansible.com/ansible/latest/modules/influxdb_database_module.html

更新:

我已经能够找到一种使用 api 而不是模块与 influx DB 交互的方法,因为它们不起作用。这涉及编辑 influxdb.conf 以启用使用 API

 # modifying the influxdb.conf is required to be able to use the influxDB API
- name: Enable http
  lineinfile:
    path: /etc/influxdb/influxdb.conf
    regexp: 'Determines whether HTTP endpoint is enabled.'
    line: '  enabled = true'
- name: Enable bind address :8086
  lineinfile:
    path: /etc/influxdb/influxdb.conf
    regexp: '# bind-address = ":8086"'
    line: '  bind-address = ":8086"'
- name: Restart influxdb,
  systemd:
    name: influxdb
    state: restarted
- name: Create influxDB database via api
  uri:
    url: "http://localhost:8086/query"
    method: POST
    body: 'q=CREATE DATABASE "grafanadb"'
    body_format: form-urlencoded
- name: create root user in influxdb
  uri:
    url: "http://localhost:8086/query"
    method: POST
    body: "q=CREATE USER user WITH PASSWORD 'pass' WITH ALL PRIVILEGES"
- name: create grafana user in influxdb
  uri:
    url: "http://localhost:8086/query"
    method: POST
    body: "q=CREATE USER grafana WITH PASSWORD 'grafana'"
- name: Grant all privileges to grafana user on grafanadb
  uri:
    url: "http://localhost:8086/query"
    method: POST
    body: "q=GRANT ALL ON grafanadb TO grafana"
    body_format: form-urlencoded

推荐阅读