Ansible:尝试使用influxdb_write时,无法在本地主机上导入所需的Python库influxdb

问题描述

我在本地计算机上运行了以下有趣的剧本和influxdb。 我只是想尝试使用ansible将一些内容写入本地influxdb。这里的想法是创建一个名为connection的表,其中包含host,region和time列。

---
- name: Influx test
  hosts: PE
  gather_facts: false

  tasks:
  - name: Write points into database
    influxdb_write:
      hostname: "localhost"
      database_name: "test"
      data_points:
        - measurement: connections
          tags:
            host: "{{inventory_hostname}}"
            region: test-region
          time: "test time"

PE看起来像这样:

[PE]
local ansible_host=localhost ansible_connection=local hostname=Lab_R1 ansible_python_interpreter=/usr/bin/python3

事实证明,当我运行剧本时,我得到了:

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_influxdb_write_payload_v07tkyyd/ansible_influxdb_write_payload.zip/ansible/module_utils/influxdb.py",line 23,in <module>
    from influxdb import InfluxDBClient
ModuleNotFoundError: No module named 'influxdb'
fatal: [local]: Failed! => {
    "changed": false,"invocation": {
        "module_args": {
            "data_points": [
                {
                    "measurement": "connections","tags": {
                        "host": "local","region": "test-region"
                    },"time": "test time"
                }
            ],"database_name": "test","hostname": "localhost","password": "root","port": 8086,"proxies": {},"retries": 3,"ssl": false,"timeout": null,"udp_port": 4444,"use_udp": false,"username": "root","validate_certs": true
        }
    },"msg": "Failed to import the required Python library (influxdb) on localhost.localdomain's Python /usr/bin/python3. 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"
}

解决方法

influxdb_write module requirements中所述,需要python库influxdb> = 0.9。

此任务的目标(在您的情况下为localhost中)是必需的。

因此,您需要使用自己选择的方法install influxdb python library(系统软件包或pip)。