Repository: ansible/ansible-modules-extras

Issue:
kubernetes.py ImportError: No module named yaml
<!--- 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

kubernetes.py
##### ANSIBLE VERSION

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

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

```
##### CONFIGURATION

<!---

-->
##### OS / ENVIRONMENT

<!---
Red Hat Enterprise Linux Server release 7.2 (Maipo)
-->
##### SUMMARY

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

Attempting to run the kubernetes module to create a namespace on my cluster.
##### 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 -->

```
 hosts: kubmst01rhel7
  remote_user: root
  tasks:
    - name: Create Wordpress Namespace
      kubernetes:
        api_endpoint: 192.168.122.40
        insecure: true
        inline_data:
          kind: Namespace
          apiVersion: v1
          metadata:
            name: wordpress-demo
        state: present

```

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

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

Creation of a namespace on my kubernetes cluster
##### ACTUAL RESULTS

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

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

```
fatal: [kubmst01rhel7]: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_5_IWnL/ansible_module_kubernetes.py\", line 153, in <module>\r\n    import yaml\r\nImportError: No module named yaml\r\n", "msg": "MODULE FAILURE"}

```



Discussion:
<!-- @ahuffman: Thanks for filing this bug report! -->

@supertom ping, this issue is waiting for your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_bug_new --->

To resolve that problem you must assure that target host has PyYAML library.
Depending on system it may come from RPM/DEB package, or you can use pip.

---

In my opinion documentation should be clarified to inform users that target host must have `PyYAML` module installed.

Also module should check whether it has `PyYAML` installed and display 'nice' error in case it's not, like other modules do, e.g.
https://github.com/ansible/ansible-modules-extras/blob/1e290f0f8fcc03eda2d7606d721d67d757804189/cloud/cloudstack/cs_facts.py#L211

@supertom, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->


Resolving patch:
diff --git a/clustering/kubernetes.py b/clustering/kubernetes.py
--- a/clustering/kubernetes.py
+++ b/clustering/kubernetes.py
@@ -150,9 +150,14 @@
             phase: "Active"
 '''
 
-import yaml
 import base64
 
+try:
+    import yaml
+    has_lib_yaml = True
+except ImportError:
+    has_lib_yaml = False
+
 ############################################################################
 ############################################################################
 # For API coverage, this Anislbe module provides capability to operate on
@@ -325,6 +330,9 @@ def main():
         required_one_of = (('file_reference', 'inline_data'),),
     )
 
+    if not has_lib_yaml:
+        module.fail_json(msg="missing python library: yaml")
+
     decode_cert_data(module)
 
     api_endpoint = module.params.get('api_endpoint')


Test patch: