ImportError:没有名为influxdb的模块。无法导入所需的Python库(influxdb)

时间:2020-08-09 21:22:58

标签: python-3.x ansible influxdb

我在使用“ influxdb_database”模块通过ansible管理influxdb时遇到问题。即使它打印输出有关python依赖关系的错误,但仅当运行insible playbook的容器在托管influxdb的diff VM上运行时,它才会失败。我从docker容器运行ansible剧本,如果我在安装了influxdb的主机上运行容器,则可以正常工作-它正在设法创建数据库。但是,当同一容器(从与前面提到的图像相同的图像创建)在与承载influxdb的容器不同的VM上运行时,它会因以下错误而失败。因此,我现在对有关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

可以登录失败的任务

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

4 个答案:

答案 0 :(得分:2)

听起来像您正在处理与我相同的问题。我努力找出问题所在,然后阅读了要求并看到了。我使用的Influxdb版本比该模块所支持的版本新,所以我收到与您相同的错误

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

python> = 2.6

influxdb> = 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

答案 1 :(得分:1)

为了使influxdb_database模块正常工作,必须确保在计算机上安装了influxdb-python。还建议安装influxdb,因为您可能希望直接从命令行访问数据库。

对于CentOS7 / RHEL7安装,可以按以下步骤完成:

brew install tree

CentOS8 / RHEL8:

yum install python-pip
pip install influxdb

注意:您必须使用其他python版本,因为CentOS7和8的默认python解释器有所不同。因此influxdb_database python解释器也将有所不同。

因此,剧本看起来像这样:

dnf install python3-pip
pip3 install influxdb

对于debian / ubuntu设置,您可以执行以下操作:

- name: Install applications for CentOS 7
  yum: 
    name:
    - influxdb        
    - python-pip

- name: Install applications for CentOS 8
  yum: 
    name:
    - influxdb        
    - python3-pip

- name: Install required pip packages
  pip:
    name: 
    - influxdb

apt-get install python-influxdb

如果您尝试连接到远程InfluxDB,则应确保您正在通过SSL进行身份验证。您必须手动启用它,因为默认情况下未启用它。

这就是远程流入数据库创建的样子:

- name: Install applications for CentOS
  apt: 
    name:
    - python-influxdb

注意:我已经使用CentOS7 / 8测试了此设置。它也可以在Ubuntu / Debian安装程序中正常工作。由于某些原因,CentOS7要求我禁用validate_certs,否则它将失败。可能是一个错误。

经过测试的版本:

  • 数据库:InfluxDB 1.8.3版
  • Ansible:2.9版

答案 2 :(得分:0)

我所缺少的是将influxdb安装在目标主机中。安装后,influxdb ansible模块开始正常工作。

答案 3 :(得分:0)

也在这个问题上挣扎。降级 requests python 包的版本对我有帮助。

<块引用>

pip 安装请求==2.6.0

(2.25.1 对我不起作用)