skip to Main Content

I have problem to manage influxdb through ansible using "influxdb_database" module. Even though it printouts error about python dependency, it is failing only when container where ansible playbook runs is run on diff VM from one where influxdb is hosted. I run ansible playbook from docker container, and if I run container on the host where influxdb is installed, it works fine – it is managing to create db. But, when same container (created from same image as one mentioned before) runs on different VM from one where influxdb is hosted, it is failing with error pasted below. So I am confused now with the error about python dependency and do not understand where the problem is.

Ansible playbook:

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 log on failed task

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 2020rndebug1: Reading configuration data /etc/ssh/ssh_configrndebug2: resolve_canonicalize: hostname 10.246.44.196 is addressrndebug1: auto-mux: Trying existing masterrndebug2: fd 3 setting O_NONBLOCKrndebug2: mux_client_hello_exchange: master version 4rndebug3: mux_client_forwards: request forwardings: 0 local, 0 remoterndebug3: mux_client_request_session: enteringrndebug3: mux_client_request_alive: enteringrndebug3: mux_client_request_alive: done pid = 2147rndebug3: mux_client_request_session: session request sentrndebug3: mux_client_read_packet: read header failed: Broken piperndebug2: Received exit status from master 1rn')
<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

Answers


  1. Chosen as BEST ANSWER

    What I was missing is influxdb installed in targeted host.Once it is installed, influxdb ansible module start working fine.


  2. sounds like you’re dealing with the same issue as me. I was struggling to find out what was wrong then I read the requirements and saw this. I’m using a newer version of influxdb than this module supports so I get the same error as you

    Requirements
    The below requirements are needed on the host that executes this module.

    python >= 2.6

    influxdb >= 0.9 & <= 1.2.4

    requests

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

    UPDATE:

    I have been able to find a way to interact with influx DB using the api instead of the modules since they don’t work. This involves editing the influxdb.conf to enable using the 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
    
    Login or Signup to reply.
  3. In order to get influxdb_database module working you have to make sure you have influxdb-python installed on your machine. It is also recommended to have influxdb installed as you might want to directly access the database from the command line.

    For CentOS7/RHEL7 installations this can be done as follows:

    yum install python-pip
    pip install influxdb
    

    CentOS8/RHEL8:

    dnf install python3-pip
    pip3 install influxdb
    

    Note: You have to use a different python version as the default python interpreter is different for CentOS7 and 8. Therefore influxdb_database python interpreter will be different too.

    Therefore, the playbook would look something like this:

    - 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
    

    For debian/ubuntu setups you might do the following:

    apt-get install python-influxdb
    

    or

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

    If you are trying to connect to a remote InfluxDB you should ensure that you are authenticating over SSL. You have to manually enable this as it is not enabled by default.

    This is what a remote influx database creation would thus look like:

    - name: Create database using custom credentials
      influxdb_database:
          hostname: "{{influxurl}}"
          username: "{{influxusername}}"
          password: "{{influxpassword}}"
          database_name: "{{influxdbv7}}"
          port: "{{influxport}}"
          ssl: yes
          validate_certs: yes 
    

    Note: I have tested this setup with CentOS7/8. It possibly works fine with Ubuntu/Debian setups too. For some reason CentOS7 required me to disable validate_certs, otherwise it fails. Possibly its a bug.

    Tested version:

    • Database: InfluxDB version 1.8.3
    • Ansible: version 2.9
    Login or Signup to reply.
  4. Also struggled with this issue. Downgrading the version of the requests python package helped me.

    pip install requests==2.6.0

    (2.25.1 did not work for me)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search