Repository: ansible/ansible

Issue:
K8S_AUTH_API_KEY as variable on the command is not working and authentication fails
##### SUMMARY
The K8S_AUTH_API_KEY, that at the moment is not working as expected, that means that authentication fails as Ansible can _read_ that key as variable. It only works if we put the value as a variable within the Ansible playbook that we are running.

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
clustering/k8s/k8s.py

##### ANSIBLE VERSION
```
ansible 2.6.0
  config file = None
  configured module search path = [u'/Users/atsitsigkos/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/2.6.0/libexec/lib/python2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.15 (default, Jun 17 2018, 12:46:58)
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->

##### OS / ENVIRONMENT
n/a

##### STEPS TO REPRODUCE
```
ansible-playbook site-namespace.yml
```

##### EXPECTED RESULTS
successful

##### ACTUAL RESULTS
```
TASK [openshift-namespace : Create or update sample namespace ] ***
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'str' object has no attribute 'get'
failed: [127.0.0.1] (item=namespace) => {"changed": false, "item": "namespace", "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_IIvIvc/ansible_module_openshift_raw.py\", line 165, in <module>\n    main()\n  File \"/tmp/ansible_IIvIvc/ansible_module_openshift_raw.py\", line 161, in main\n    KubernetesRawModule().execute_module()\n  File \"/tmp/ansible_IIvIvc/ansible_modlib.zip/ansible/module_utils/k8s/raw.py\", line 70, in execute_module\n  File \"/tmp/ansible_IIvIvc/ansible_modlib.zip/ansible/module_utils/k8s/common.py\", line 167, in get_api_client\n  File \"/usr/lib/python2.7/site-packages/openshift/dynamic/client.py\", line 58, in __init__\n    self._load_server_info()\n  File \"/usr/lib/python2.7/site-packages/openshift/dynamic/client.py\", line 62, in _load_server_info\n    self.__version = {'kubernetes': load_json(self.request('get', '/version'))}\n  File \"/usr/lib/python2.7/site-packages/openshift/dynamic/client.py\", line 255, in request\n    _return_http_data_only=params.get('_return_http_data_only', True)\n  File \"/usr/lib/python2.7/site-packages/kubernetes/client/api_client.py\", line 321, in call_api\n    _return_http_data_only, collection_formats, _preload_content, _request_timeout)\n  File \"/usr/lib/python2.7/site-packages/kubernetes/client/api_client.py\", line 140, in __call_api\n    self.update_params_for_auth(header_params, query_params, auth_settings)\n  File \"/usr/lib/python2.7/site-packages/kubernetes/client/api_client.py\", line 499, in update_params_for_auth\n    auth_setting = self.configuration.auth_settings().get(auth)\n  File \"/usr/lib/python2.7/site-packages/kubernetes/client/configuration.py\", line 228, in auth_settings\n    'value': self.get_api_key_with_prefix('authorization')\n  File \"/usr/lib/python2.7/site-packages/kubernetes/client/configuration.py\", line 202, in get_api_key_with_prefix\n    if self.api_key.get(identifier) and self.api_key_prefix.get(identifier):\nAttributeError: 'str' object has no attribute 'get'\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'str' object has no attribute 'get'
```



Discussion:
Files identified in the description:
None

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 --->
Files identified in the description:
* [lib/ansible/modules/files/file.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/files/file.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 --->
Files identified in the description:
* [lib/ansible/modules/clustering/k8s/k8s.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/clustering/k8s/k8s.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 @chouseknecht @fabianvf @flaper87 @maxamillion
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
could you paste site-namespace.yml ?
A very basic one: 

- name: Create a k8s namespace
  k8s:
    name: namespace
    api_version: v1
    kind: Namespace
    state: present

Resolving patch:
diff --git a/lib/ansible/module_utils/k8s/common.py b/lib/ansible/module_utils/k8s/common.py
--- a/lib/ansible/module_utils/k8s/common.py
+++ b/lib/ansible/module_utils/k8s/common.py
@@ -148,8 +148,11 @@ def get_api_client(self, **auth_params):
             elif key in auth_args and value is None:
                 env_value = os.getenv('K8S_AUTH_{0}'.format(key.upper()), None)
                 if env_value is not None:
-                    setattr(configuration, key, env_value)
-                    auth[key] = env_value
+                    if key == 'api_key':
+                        setattr(configuration, key, {'authorization': "Bearer {0}".format(env_value)})
+                    else:
+                        setattr(configuration, key, env_value)
+                        auth[key] = env_value
 
         kubernetes.client.Configuration.set_default(configuration)
 


Test patch: