Repository: ansible/ansible

Issue:
Creating PersistentVolume with k8s_raw fails
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and devel branch are affected too.
Always add information AFTER of these html comments.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report

##### COMPONENT NAME
<!---
Insert below this comment the 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
-->
k8s_raw

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/kube/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste 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
<!---
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.
-->
Ubuntu 16.04 x64

##### SUMMARY
<!--- Explain the problem briefly -->
Creating or querying a PersistentVolume fails.

##### 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.
-->
Run the below playbook against the k8s master node, ensuring you have a working `~/.kube/config`
<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- hosts: all
  tasks:
  - k8s_raw:
      state: present
      definition:
        apiVersion: v1
        kind: PersistentVolume
        metadata:
          name: test
          namespace: test
        spec:
          capacity:
            storage: 3Ti
          accessModes:
          - ReadWriteMany
          persistentVolumeReclaimPolicy: Recycle
          nfs:
            server: 1.2.3.4
            path: /Share
```
```
ansible-playbook -i localhost, -c local pv.yaml
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Expected a new PersistentVolume to be created.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The below error occured.

<!--- Paste verbatim command output between quotes below -->
```
kube@k8snode0:/tmp$ ansible-playbook -vvvv -i localhost, -c local  pv.yaml
ansible-playbook 2.5.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/kube/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.12 (default, Dec  4 2017, 14:50:18) [GCC 5.4.0 20160609]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
Set default localhost to localhost
Parsed localhost, inventory source with host_list plugin
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/dist-packages/ansible/plugins/callback/default.pyc

PLAYBOOK: pv.yaml ***************************************************************************************************************************************************************************************************************************
1 plays in pv.yaml

PLAY [all] **********************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
task path: /tmp/pv.yaml:2
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/system/setup.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: kube
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/kube/.ansible/tmp/ansible-tmp-1522151462.33-181544822931141 `" && echo ansible-tmp-1522151462.33-181544822931141="` echo /home/kube/.ansible/tmp/ansible-tmp-1522151462.33-181544822931141 `" ) && sleep 0'
<localhost> PUT /home/kube/.ansible/tmp/ansible-local-32190wIJQEP/tmpAXYhoy TO /home/kube/.ansible/tmp/ansible-tmp-1522151462.33-181544822931141/setup.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/kube/.ansible/tmp/ansible-tmp-1522151462.33-181544822931141/ /home/kube/.ansible/tmp/ansible-tmp-1522151462.33-181544822931141/setup.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/kube/.ansible/tmp/ansible-tmp-1522151462.33-181544822931141/setup.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /home/kube/.ansible/tmp/ansible-tmp-1522151462.33-181544822931141/ > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers

TASK [k8s_raw] ******************************************************************************************************************************************************************************************************************************
task path: /tmp/pv.yaml:5
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/clustering/k8s/k8s_raw.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: kube
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/kube/.ansible/tmp/ansible-tmp-1522151463.48-74887846299278 `" && echo ansible-tmp-1522151463.48-74887846299278="` echo /home/kube/.ansible/tmp/ansible-tmp-1522151463.48-74887846299278 `" ) && sleep 0'
<localhost> PUT /home/kube/.ansible/tmp/ansible-local-32190wIJQEP/tmp_5oVRz TO /home/kube/.ansible/tmp/ansible-tmp-1522151463.48-74887846299278/k8s_raw.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/kube/.ansible/tmp/ansible-tmp-1522151463.48-74887846299278/ /home/kube/.ansible/tmp/ansible-tmp-1522151463.48-74887846299278/k8s_raw.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/kube/.ansible/tmp/ansible-tmp-1522151463.48-74887846299278/k8s_raw.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /home/kube/.ansible/tmp/ansible-tmp-1522151463.48-74887846299278/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
  File "/tmp/ansible_z6yHm4/ansible_modlib.zip/ansible/module_utils/k8s/raw.py", line 100, in execute_module
    existing = self.helper.get_object(name, namespace)
  File "/usr/local/lib/python2.7/dist-packages/openshift/helper/base.py", line 195, in get_object
    get_method = self.lookup_method(method_name, namespace)
  File "/usr/local/lib/python2.7/dist-packages/openshift/helper/base.py", line 458, in lookup_method
    "Error: method {0} not found for model {1}. {2}".format(method_name, self.kind, msg)

fatal: [localhost]: FAILED! => {
    "changed": false,
    "error": null,
    "invocation": {
        "module_args": {
            "api_key": null,
            "cert_file": null,
            "context": null,
            "host": null,
            "key_file": null,
            "kubeconfig": null,
            "name": "test",
            "namespace": "test",
            "password": null,
            "spec_access_modes": [
                "ReadWriteMany"
            ],
            "spec_capacity": {
                "storage": "3Ti"
            },
            "spec_nfs_path": "/Share",
            "spec_nfs_server": "1.2.3.4",
            "spec_persistent_volume_reclaim_policy": "Recycle",
            "ssl_ca_cert": null,
            "username": null,
            "verify_ssl": null
        }
    },
    "msg": "Failed to retrieve requested object: Error: method read_namespaced_persistent_volume not found for model persistent_volume. "
}
        to retry, use: --limit @/tmp/pv.retry

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/ec2.py b/lib/ansible/modules/cloud/amazon/ec2.py
--- a/lib/ansible/modules/cloud/amazon/ec2.py
+++ b/lib/ansible/modules/cloud/amazon/ec2.py
@@ -134,7 +134,6 @@
     description:
       - when provisioning within vpc, assign a public IP address. Boto library must be 2.13.0+
     type: bool
-    default: 'no'
   private_ip:
     version_added: "1.2"
     description:
@@ -1560,7 +1559,7 @@ def warn_if_public_ip_assignment_changed(module, instance):
 
     # Check that public ip assignment is the same and warn if not
     public_dns_name = getattr(instance, 'public_dns_name', None)
-    if (assign_public_ip or public_dns_name) and (not public_dns_name or not assign_public_ip):
+    if (assign_public_ip or public_dns_name) and (not public_dns_name or assign_public_ip is False):
         module.warn("Unable to modify public ip assignment to {0} for instance {1}. "
                     "Whether or not to assign a public IP is determined during instance creation.".format(assign_public_ip, instance.id))
 
@@ -1590,7 +1589,7 @@ def main():
             user_data=dict(),
             instance_tags=dict(type='dict'),
             vpc_subnet_id=dict(),
-            assign_public_ip=dict(type='bool', default=False),
+            assign_public_ip=dict(type='bool'),
             private_ip=dict(),
             instance_profile_name=dict(),
             instance_ids=dict(type='list', aliases=['instance_id']),


Test patch: