Repository: ansible-collections/community.general

Issue:
influxdb_user: can't create user with no existing users and auth enabled
### Summary

When deploying a new InfluxDB service with no existing users and authentication enabled, InfluxDB 1.x allows to execute a `CREATE USER` query but no other queries. This is specified in the official docs (https://docs.influxdata.com/influxdb/v1.8/administration/authentication_and_authorization/):

> If you enable authentication and have no users, InfluxDB will not enforce authentication and will only accept the query that creates a new admin user.

No other query is allowed and, because of this, `influxdb_user` module fails as it tries to get existing users first to ensure idempotent execution.

### Issue Type

Bug Report

### Component Name

influxdb_user

### Ansible Version

```console (paste below)
$ ansible --version
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0]
```

### Configuration

```console (paste below)
$ ansible-config dump --only-changed
```


### OS / Environment

Tested on a fresh Ubuntu 20.04 installation and also on macOS Big Sur 11.2.3.

InfluxDB is running on a Docker container with version 1.8.5 and it's a fresh container. Has also been tested with Vagrant on an Ubuntu 20.04 box to discard possible issues with the container.

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
- hosts: localhost
  gather_facts: true
  tasks:
    - community.general.influxdb_user:
        user_name: root
        user_password: test
        admin: true
```


### Expected Results

No error should be thrown and a new user should be created.

### Actual Results

```console (paste below)
$ ansible-playbook influx.yml

PLAY [localhost] **********************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [localhost]

TASK [community.general.influxdb_user] ************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "403: {\"error\":\"error authorizing query: create admin user first or disable authentication\"}"}

PLAY RECAP ****************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/database/influxdb/influxdb_user.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/database/influxdb/influxdb_user.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 @zhhuta
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/database/influxdb/influxdb_user.py b/plugins/modules/database/influxdb/influxdb_user.py
--- a/plugins/modules/database/influxdb/influxdb_user.py
+++ b/plugins/modules/database/influxdb/influxdb_user.py
@@ -100,6 +100,8 @@
 #only defaults
 '''
 
+import json
+
 from ansible.module_utils.urls import ConnectionError
 from ansible.module_utils.basic import AnsibleModule
 from ansible.module_utils._text import to_native
@@ -115,7 +117,7 @@ def find_user(module, client, user_name):
             if user['user'] == user_name:
                 user_result = user
                 break
-    except (ConnectionError, influx.exceptions.InfluxDBClientError) as e:
+    except ConnectionError as e:
         module.fail_json(msg=to_native(e))
     return user_result
 
@@ -198,6 +200,9 @@ def set_user_grants(module, client, user_name, grants):
     return changed
 
 
+INFLUX_AUTH_FIRST_USER_REQUIRED = "error authorizing query: create admin user first or disable authentication"
+
+
 def main():
     argument_spec = influx.InfluxDb.influxdb_argument_spec()
     argument_spec.update(
@@ -219,7 +224,23 @@ def main():
     grants = module.params['grants']
     influxdb = influx.InfluxDb(module)
     client = influxdb.connect_to_influxdb()
-    user = find_user(module, client, user_name)
+
+    user = None
+    try:
+        user = find_user(module, client, user_name)
+    except influx.exceptions.InfluxDBClientError as e:
+        if e.code == 403:
+            reason = None
+            try:
+                msg = json.loads(e.content)
+                reason = msg["error"]
+            except (KeyError, ValueError):
+                module.fail_json(msg=to_native(e))
+
+            if reason != INFLUX_AUTH_FIRST_USER_REQUIRED:
+                module.fail_json(msg=to_native(e))
+        else:
+            module.fail_json(msg=to_native(e))
 
     changed = False
 


Test patch: