Repository: ansible/ansible

Issue:
influxdb_user module fails with "KeyError: 'database_name'"
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report

##### COMPONENT NAME
<!---
Name of the module, plugin, task or feature
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path
-->
influxdb_user

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.0 (3237-change-rabbitmq-user-password-without-force 4cdda0d9a3) last updated 2018/01/23 21:56:35 (GMT +300)
  config file = /home/art/.ansible.cfg
  configured module search path = [u'/home/art/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/art/projects/github/ansible/lib/ansible
  executable location = /home/art/projects/github/ansible/bin/ansible
  python version = 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609]
```

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
N/A

##### SUMMARY
<!--- Explain the problem briefly -->
influxdb_user module fails with "KeyError: 'database_name'" after #34021.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Create admin user
  influxdb_user:
    user_name: '{{ influxdb_admin_user }}'
    user_password: '{{ influxdb_admin_password }}'
    admin: yes
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
fatal: [host]: FAILED! => {"attempts": 1, "changed": false, "failed": true, "module_stderr": "Shared connection to host closed.\r\n", "module_stdout": "\r\nTraceback (most recent call last):\r\n  File \"/tmp/ansible_IYpuet/ansible_module_influxdb_user.py\", line 156, in <module>\r\n    main()\r\n  File \"/tmp/ansible_IYpuet/ansible_module_influxdb_user.py\", line 138, in main\r\n    influxdb = InfluxDb(module)\r\n  File \"/tmp/ansible_IYpuet/ansible_modlib.zip/ansible/module_utils/influxdb.py\", line 31, in __init__\r\nKeyError: 'database_name'\r\n", "msg": "MODULE FAILURE", "rc": 1}
```


Discussion:
Files identified in the description:
* [lib/ansible/modules/database/influxdb/influxdb_user.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/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/lib/ansible/module_utils/influxdb.py b/lib/ansible/module_utils/influxdb.py
--- a/lib/ansible/module_utils/influxdb.py
+++ b/lib/ansible/module_utils/influxdb.py
@@ -28,7 +28,7 @@ def __init__(self, module):
         self.port = self.params['port']
         self.username = self.params['username']
         self.password = self.params['password']
-        self.database_name = self.params['database_name']
+        self.database_name = self.params.get('database_name')
 
     def check_lib(self):
         if not HAS_REQUESTS:
diff --git a/lib/ansible/modules/database/influxdb/influxdb_user.py b/lib/ansible/modules/database/influxdb/influxdb_user.py
--- a/lib/ansible/modules/database/influxdb/influxdb_user.py
+++ b/lib/ansible/modules/database/influxdb/influxdb_user.py
@@ -125,7 +125,6 @@ def main():
         user_name=dict(required=True, type='str'),
         user_password=dict(required=False, type='str', no_log=True),
         admin=dict(default='False', type='bool')
-
     )
     module = AnsibleModule(
         argument_spec=argument_spec,


Test patch: