Repository: ansible-collections/community.general

Issue:
 influxdb_database got an unexpected keyword argument 'path'
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
The influxdb_database module fails to execute on remote hosts with a TypeError.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
influxdb_database

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.10.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/exploit/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.8/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.5 (default, Sep  5 2020, 10:50:12) [GCC 10.2.0]
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below

```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->
Remote OS is ubuntu 16.04 with both Python2.7 and Python3.5
Ansible is launched from an ArchLinux machine using Python3.8

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---

- hosts: all
  gather_facts: no
  tasks:
    - influxdb_database:
        hostname: localhost
        database_name: foobar
        port: 8086
        state: present
```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
Module should execute and create the required database in the local InfluxDB instance.

Works fine in Ansible 2.9

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->
Module fails to execute and raises a `TypeError got an unexpected keyword argument 'path'`.

<!--- Paste verbatim command output between quotes -->
```paste below
$ ansible-playbook -i 172.20.42.11, mcve.yml -k -vvv
ansible-playbook 2.10.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/exploit/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.8/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.8.5 (default, Sep  5 2020, 10:50:12) [GCC 10.2.0]
Using /etc/ansible/ansible.cfg as config file
SSH password: 
Parsed 172.20.42.11, inventory source with host_list plugin
redirecting (type: modules) ansible.builtin.influxdb_database to community.general.influxdb_database

PLAYBOOK: mcve.yml ****************************************************************************************************************************************************************************************************************************
1 plays in mcve.yml

PLAY [all] ************************************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [influxdb_database] **********************************************************************************************************************************************************************************************************************
task path: /home/exploit/code_testing/ansible/mcve.yml:6
<172.20.42.11> ESTABLISH SSH CONNECTION FOR USER: None
<172.20.42.11> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/exploit/.ansible/cp/9bfdd0dc66 172.20.42.11 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<172.20.42.11> (0, b'/home/exploit\n', b'')
<172.20.42.11> ESTABLISH SSH CONNECTION FOR USER: None
<172.20.42.11> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/exploit/.ansible/cp/9bfdd0dc66 172.20.42.11 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/exploit/.ansible/tmp `"&& mkdir "` echo /home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832 `" && echo ansible-tmp-1601368297.2341213-9414-127682684873832="` echo /home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832 `" ) && sleep 0'"'"''
<172.20.42.11> (0, b'ansible-tmp-1601368297.2341213-9414-127682684873832=/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832\n', b'')
redirecting (type: modules) ansible.builtin.influxdb_database to community.general.influxdb_database
<172.20.42.11> Attempting python interpreter discovery
<172.20.42.11> ESTABLISH SSH CONNECTION FOR USER: None
<172.20.42.11> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/exploit/.ansible/cp/9bfdd0dc66 172.20.42.11 '/bin/sh -c '"'"'echo PLATFORM; uname; echo FOUND; command -v '"'"'"'"'"'"'"'"'/usr/bin/python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.5'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/libexec/platform-python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/bin/python3'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python'"'"'"'"'"'"'"'"'; echo ENDFOUND && sleep 0'"'"''
<172.20.42.11> (0, b'PLATFORM\nLinux\nFOUND\n/usr/bin/python\n/usr/bin/python3.5\n/usr/bin/python2.7\n/usr/bin/python3\n/usr/bin/python\nENDFOUND\n', b'')
<172.20.42.11> ESTABLISH SSH CONNECTION FOR USER: None
<172.20.42.11> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/exploit/.ansible/cp/9bfdd0dc66 172.20.42.11 '/bin/sh -c '"'"'/usr/bin/python && sleep 0'"'"''
<172.20.42.11> (0, b'{"osrelease_content": "NAME=\\"Ubuntu\\"\\nVERSION=\\"16.04.6 LTS (Xenial Xerus)\\"\\nID=ubuntu\\nID_LIKE=debian\\nPRETTY_NAME=\\"Ubuntu 16.04.6 LTS\\"\\nVERSION_ID=\\"16.04\\"\\nHOME_URL=\\"http://www.ubuntu.com/\\"\\nSUPPORT_URL=\\"http://help.ubuntu.com/\\"\\nBUG_REPORT_URL=\\"http://bugs.launchpad.net/ubuntu/\\"\\nVERSION_CODENAME=xenial\\nUBUNTU_CODENAME=xenial\\n", "platform_dist_result": ["Ubuntu", "16.04", "xenial"]}\n', b'')
Using module file /usr/lib/python3.8/site-packages/ansible_collections/community/general/plugins/modules/influxdb_database.py
<172.20.42.11> PUT /home/exploit/.ansible/tmp/ansible-local-9410j_m2k00u/tmpmv0_dg5o TO /home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py
<172.20.42.11> SSH: EXEC sshpass -d10 sftp -o BatchMode=no -b - -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/exploit/.ansible/cp/9bfdd0dc66 '[172.20.42.11]'
<172.20.42.11> (0, b'sftp> put /home/exploit/.ansible/tmp/ansible-local-9410j_m2k00u/tmpmv0_dg5o /home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py\n', b'')
<172.20.42.11> ESTABLISH SSH CONNECTION FOR USER: None
<172.20.42.11> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/exploit/.ansible/cp/9bfdd0dc66 172.20.42.11 '/bin/sh -c '"'"'chmod u+x /home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/ /home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py && sleep 0'"'"''
<172.20.42.11> (0, b'', b'')
<172.20.42.11> ESTABLISH SSH CONNECTION FOR USER: None
<172.20.42.11> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/exploit/.ansible/cp/9bfdd0dc66 -tt 172.20.42.11 '/bin/sh -c '"'"'/usr/bin/python /home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py && sleep 0'"'"''
<172.20.42.11> (1, b'Traceback (most recent call last):\r\n  File "/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py", line 102, in <module>\r\n    _ansiballz_main()\r\n  File "/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py", line 94, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File "/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py", line 40, in invoke_module\r\n    runpy.run_module(mod_name=\'ansible_collections.community.general.plugins.modules.influxdb_database\', init_globals=None, run_name=\'__main__\', alter_sys=True)\r\n  File "/usr/lib/python2.7/runpy.py", line 188, in run_module\r\n    fname, loader, pkg_name)\r\n  File "/usr/lib/python2.7/runpy.py", line 82, in _run_module_code\r\n    mod_name, mod_fname, mod_loader, pkg_name)\r\n  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code\r\n    exec code in run_globals\r\n  File "/tmp/ansible_influxdb_database_payload_k1Cvhm/ansible_influxdb_database_payload.zip/ansible_collections/community/general/plugins/modules/influxdb_database.py", line 141, in <module>\r\n  File "/tmp/ansible_influxdb_database_payload_k1Cvhm/ansible_influxdb_database_payload.zip/ansible_collections/community/general/plugins/modules/influxdb_database.py", line 123, in main\r\n  File "/tmp/ansible_influxdb_database_payload_k1Cvhm/ansible_influxdb_database_payload.zip/ansible_collections/community/general/plugins/module_utils/influxdb.py", line 88, in connect_to_influxdb\r\nTypeError: __init__() got an unexpected keyword argument \'path\'\r\n', b'Shared connection to 172.20.42.11 closed.\r\n')
<172.20.42.11> Failed to connect to the host via ssh: Shared connection to 172.20.42.11 closed.
<172.20.42.11> ESTABLISH SSH CONNECTION FOR USER: None
<172.20.42.11> SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/home/exploit/.ansible/cp/9bfdd0dc66 172.20.42.11 '/bin/sh -c '"'"'rm -f -r /home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/ > /dev/null 2>&1 && sleep 0'"'"''
<172.20.42.11> (0, b'', b'')
The full traceback is:
Traceback (most recent call last):
  File "/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py", line 102, in <module>
    _ansiballz_main()
  File "/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py", line 94, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py", line 40, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.influxdb_database', init_globals=None, run_name='__main__', alter_sys=True)
  File "/usr/lib/python2.7/runpy.py", line 188, in run_module
    fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 82, in _run_module_code
    mod_name, mod_fname, mod_loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/tmp/ansible_influxdb_database_payload_k1Cvhm/ansible_influxdb_database_payload.zip/ansible_collections/community/general/plugins/modules/influxdb_database.py", line 141, in <module>
  File "/tmp/ansible_influxdb_database_payload_k1Cvhm/ansible_influxdb_database_payload.zip/ansible_collections/community/general/plugins/modules/influxdb_database.py", line 123, in main
  File "/tmp/ansible_influxdb_database_payload_k1Cvhm/ansible_influxdb_database_payload.zip/ansible_collections/community/general/plugins/module_utils/influxdb.py", line 88, in connect_to_influxdb
TypeError: __init__() got an unexpected keyword argument 'path'
[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host 172.20.42.11 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the 
discovered platform python for this host. See https://docs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation warnings can be disabled 
by setting deprecation_warnings=False in ansible.cfg.
fatal: [172.20.42.11]: FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "module_stderr": "Shared connection to 172.20.42.11 closed.\r\n",
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py\", line 102, in <module>\r\n    _ansiballz_main()\r\n  File \"/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py\", line 94, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/home/exploit/.ansible/tmp/ansible-tmp-1601368297.2341213-9414-127682684873832/AnsiballZ_influxdb_database.py\", line 40, in invoke_module\r\n    runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.influxdb_database', init_globals=None, run_name='__main__', alter_sys=True)\r\n  File \"/usr/lib/python2.7/runpy.py\", line 188, in run_module\r\n    fname, loader, pkg_name)\r\n  File \"/usr/lib/python2.7/runpy.py\", line 82, in _run_module_code\r\n    mod_name, mod_fname, mod_loader, pkg_name)\r\n  File \"/usr/lib/python2.7/runpy.py\", line 72, in _run_code\r\n    exec code in run_globals\r\n  File \"/tmp/ansible_influxdb_database_payload_k1Cvhm/ansible_influxdb_database_payload.zip/ansible_collections/community/general/plugins/modules/influxdb_database.py\", line 141, in <module>\r\n  File \"/tmp/ansible_influxdb_database_payload_k1Cvhm/ansible_influxdb_database_payload.zip/ansible_collections/community/general/plugins/modules/influxdb_database.py\", line 123, in main\r\n  File \"/tmp/ansible_influxdb_database_payload_k1Cvhm/ansible_influxdb_database_payload.zip/ansible_collections/community/general/plugins/module_utils/influxdb.py\", line 88, in connect_to_influxdb\r\nTypeError: __init__() got an unexpected keyword argument 'path'\r\n",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

PLAY RECAP ************************************************************************************************************************************************************************************************************************************
172.20.42.11               : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
```



Discussion:
Files identified in the description:
* [`plugins/modules/database/influxdb/influxdb_database.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/database/influxdb/influxdb_database.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @kamsz
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Which version of the InfluxDB python module (https://pypi.org/project/influxdb/) do you have installed on the target machine?
```
$ dpkg -l | grep influx
ii  influxdb                              1.7.1-1                                    amd64        Distributed time-series database.
ii  python-influxdb                       2.12.0-1                                   all          Client for InfluxDB - Python 2.7
```

I guess this could be fixed by removing everything from `args` which has value `None` (https://github.com/ansible-collections/community.general/blob/main/plugins/module_utils/influxdb.py#L69-L82), or by adding `path` only for a new enough version of the influxdb Python module. No idea which version got `path` added, but since you're using a four years old version, I guess it happened after 2.12.0.
The perks of using a new version with old remotes ^^

I’ll try using a newer version of `python-influxdb` to confirm it works.
I just installed the 5.3.0 version of the `influxdb` module on the remote and I confirm this solves the issue. I guess I’ll use the `pip` module to install it on next remotes instead of `apt` then.

For the `path` parameter, it seems to come from version 5.1.0 : https://github.com/influxdata/influxdb-python/commit/f3c6acf503e1befd767c4cd70f46d9561341c05a
So a test akin to https://github.com/ansible-collections/community.general/blob/main/plugins/module_utils/influxdb.py#L84-L86 could be used.
The following change works with my setup:
``` diff
--- influxdb.py
+++ influxdb.py
@@ -69,7 +69,6 @@
         args = dict(
             host=self.hostname,
             port=self.port,
-            path=self.path,
             username=self.username,
             password=self.password,
             database=self.database_name,
@@ -85,4 +84,8 @@
             # retries option is added in version 4.1.0
             args.update(retries=self.params['retries'])
 
+        if influxdb_api_version >= ('5', '1', '0'):
+            # path argument is added in version 5.1.0
+            args.update(path=self.path)
+
         return InfluxDBClient(**args)
```
Hey maintainers, 
would you accept the above patch or is it something your are not interested supporting ?
I'd accept such a patch :) The `influxdb_api_version` comparsion needs some work, though (and that variable doesn't exist yet). If done this way, it will for example fail if `influxdb_api_version == ('10', '0', '0')`. Better use `LooseVersion` for such comparsions (grep for it to find out how to use it).
Hey @Kniyl 

Are you interested in doing this patch ?
> I'd accept such a patch :) The `influxdb_api_version` comparsion needs some work, though (and that variable doesn't exist yet). If done this way, it will for example fail if `influxdb_api_version == ('10', '0', '0')`. Better use `LooseVersion` for such comparsions (grep for it to find out how to use it).

Not sure I understand why this variable wouldn't exist as it is defined 5 lines earlier, but sure, I can provide a PR with `LooseVersion` if need be.

Resolving patch:
diff --git a/plugins/doc_fragments/influxdb.py b/plugins/doc_fragments/influxdb.py
--- a/plugins/doc_fragments/influxdb.py
+++ b/plugins/doc_fragments/influxdb.py
@@ -40,6 +40,7 @@ class ModuleDocFragment(object):
   path:
     description:
     - The path on which InfluxDB server is accessible
+    - Only available when using python-influxdb >= 5.1.0
     type: str
     version_added: '0.2.0'
   validate_certs:
@@ -60,6 +61,7 @@ class ModuleDocFragment(object):
     description:
     - Number of retries client will try before aborting.
     - C(0) indicates try until success.
+    - Only available when using python-influxdb >= 4.1.0
     type: int
     default: 3
   use_udp:
diff --git a/plugins/module_utils/influxdb.py b/plugins/module_utils/influxdb.py
--- a/plugins/module_utils/influxdb.py
+++ b/plugins/module_utils/influxdb.py
@@ -9,6 +9,7 @@
 import traceback
 
 from ansible.module_utils.basic import missing_required_lib
+from distutils.version import LooseVersion
 
 REQUESTS_IMP_ERR = None
 try:
@@ -69,7 +70,6 @@ def connect_to_influxdb(self):
         args = dict(
             host=self.hostname,
             port=self.port,
-            path=self.path,
             username=self.username,
             password=self.password,
             database=self.database_name,
@@ -80,9 +80,13 @@ def connect_to_influxdb(self):
             udp_port=self.params['udp_port'],
             proxies=self.params['proxies'],
         )
-        influxdb_api_version = tuple(influxdb_version.split("."))
-        if influxdb_api_version >= ('4', '1', '0'):
+        influxdb_api_version = LooseVersion(influxdb_version)
+        if influxdb_api_version >= LooseVersion('4.1.0'):
             # retries option is added in version 4.1.0
             args.update(retries=self.params['retries'])
 
+        if influxdb_api_version >= LooseVersion('5.1.0'):
+            # path argument is added in version 5.1.0
+            args.update(path=self.path)
+
         return InfluxDBClient(**args)
diff --git a/plugins/modules/database/influxdb/influxdb_database.py b/plugins/modules/database/influxdb/influxdb_database.py
--- a/plugins/modules/database/influxdb/influxdb_database.py
+++ b/plugins/modules/database/influxdb/influxdb_database.py
@@ -16,7 +16,7 @@
 author: "Kamil Szczygiel (@kamsz)"
 requirements:
     - "python >= 2.6"
-    - "influxdb >= 0.9 & <= 1.2.4"
+    - "influxdb >= 0.9"
     - requests
 options:
     database_name:


Test patch: