Repository: ansible/ansible-modules-extras

Issue:
Kubernetes module error handling fails when creating Deployment (or any unsupported kind)
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### COMPONENT NAME

<!--- Name of the plugin/module/task -->

kubernetes module
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
ansible 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

gathering = explicit
host_key_checking = False
sudo_flags = -H -S
pipelining = True
retry_files_enabled = False
##### 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.
-->

CentOS Linux release 7.2.1511 (Core)
##### SUMMARY

<!--- Explain the problem briefly -->

I am trying to create a Deployment resource on my kubernetes cluster using Ansible module but it fails. Services, namespaces, etc have no problems.
Everything works out using the command line:

```
kubectl create -f deployment.yaml
```
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->

Create resource yaml deployment.yaml.

```

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
```

Create plabook main.yaml.

```

---
- name: a play that runs entirely on the ansible host
  hosts: 127.0.0.1
  connection: local
  tasks:
    - name: Install required dependencies
      yum: name=PyYAML state=present
    - name: Create deployment
      kubernetes:
        api_endpoint: 172.16.150.27:8080
        insecure: true
        file_reference: /etc/ansible/deployment.yaml
        state: present
```

Run ansible:

```
ansible-playbook main.yml
```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->

```
TASK [Create deployment] *****************************************
ok: [127.0.0.1]
```
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with high verbosity (-vvvv) -->

Task fails.

<!--- Paste verbatim command output between quotes below -->

```
TASK [Create deployment] ****************************************
task path: /var/lib/go-agent/pipelines/tbx-kubernetes-deploy/roles/deploy/tasks/main.yml:6
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python && sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_cUGiyO/ansible_module_kubernetes.py", line 392, in <module>
    main()
  File "/tmp/ansible_cUGiyO/ansible_module_kubernetes.py", line 366, in main
    module.fail_json("invalid resource kind specified in the data: '%s'" % kind)
TypeError: fail_json() takes exactly 1 argument (2 given)

fatal: [127.0.0.1]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "kubernetes"}, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_cUGiyO/ansible_module_kubernetes.py\", line 392, in <module>\n    main()\n  File \"/tmp/ansible_cUGiyO/ansible_module_kubernetes.py\", line 366, in main\n    module.fail_json(\"invalid resource kind specified in the data: '%s'\" % kind)\nTypeError: fail_json() takes exactly 1 argument (2 given)\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}
```



Discussion:

Resolving patch:
diff --git a/clustering/kubernetes.py b/clustering/kubernetes.py
--- a/clustering/kubernetes.py
+++ b/clustering/kubernetes.py
@@ -363,7 +363,7 @@ def main():
             try:
                 url = target_endpoint + KIND_URL[kind]
             except KeyError:
-                module.fail_json("invalid resource kind specified in the data: '%s'" % kind)
+                module.fail_json(msg="invalid resource kind specified in the data: '%s'" % kind)
             url = url.replace("{namespace}", namespace)
         else:
             url = target_endpoint


Test patch: