Search code examples
ansibleinfluxdb

Ansible: Failed to import the required Python library (influxdb) on localhost when trying to use influxdb_write


I've got the following ansible playbook and influxdb running on my local machine. I simply want to try writing something to my local influxdb using ansible. The idea here would be to create a table called connections with columns host, region and 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 looks like this:

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

Turns out that when I run my playbook I get:

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"
}

Solution

  • As documented in the influxdb_write module requirements, python library influxdb >= 0.9 is required.

    This requirement is needed on the target of the task, which is in your case localhost.

    So you need to install influxdb python library with the method of your choice (system package or pip).