Repository: ansible/ansible

Issue:
argument_spec: When using sub options in a module, an exception is thrown because of a 
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
module_utils/basic.py

##### ANSIBLE VERSION
```
ansible 2.4.0
  config file =
  configured module search path = [u'/Users/pdellaer/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/pdellaer/VirtualEnvs/vspk-ansible/lib/python2.7/site-packages/ansible
  executable location = /Users/pdellaer/VirtualEnvs/vspk-ansible/bin/ansible
  python version = 2.7.13 (default, Dec 17 2016, 23:03:43) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
```

##### CONFIGURATION
No changes, default configuration

##### OS / ENVIRONMENT
N/A

##### SUMMARY
When using the new sub options in the argument spec of `module_utils/basic.py`, an exception is thrown because it is looking for the sub option in the main params instead of in the sub option params 
https://github.com/ansible/ansible/blob/757758cd1a09beb7b99aef7b5dab04bb2f4fc428/lib/ansible/module_utils/basic.py#L1739


##### STEPS TO REPRODUCE
(in a fresh virtualenv)
```
git clone git@github.com:ansible/ansible.git
cd ansible
pip install .
cd ..
git clone git@github.com:nuagenetworks/vspk-ansible.git
cd vspk-ansible
ansible-playbook -vvvv nuage-vspk-negative-tests.yml
```
The above playbook will ignore errors, so you'll see the exception/traceback multiple times.

You can do it with the `nuage-vspk-tests.yml` as well, this is an actual positive test of the new module. 

The example task below is the first in the `nuage-vspk-negative-tests.yml`.

```yaml
- name: Checking auth - missing username                                                                                                                                                                                                        
  connection: local                                                                                                                                                                                                                             
  nuage_vspk:                                                                                                                                                                                                                                     
    auth:                                                                                                                                                                                                                                           
      api_password: csproot                                                                                                                                                                                                                         
      api_enterprise: csp                                                                                                                                                                                                                           
      api_url: https://localhost:8443                                                                                                                                                                                                               
      api_version: v4_0                                                                                                                                                                                                                           
    type: Job                                                                                                                                                                                                                                     
    id: dumy-job-uuid                                                                                                                                                                                                                             
    command: wait_for_job
```

##### EXPECTED RESULTS
In case of `nuage-vspk-negative-tests.yml` as playbook, you don't need VSPK or Nuage Networks VSP installed, each test should fail because of the agurment_spec validation. The result of the above yaml example (`Checking auth - missing username`, which should fail because of missing mandatory sub option) would result in:
```
TASK [nuage-vspk-negative-tests : Checking auth - missing username] **************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "missing required arguments: api_username"}
```

In case of `nuage-vspk-tests.yml` as playbook and proper installation of VSPK and having a freshly installed Nuage Networks VSP environment available, each task should succeed.

##### ACTUAL RESULTS
This is the output for the task defined in the yaml mentioned earlier, the first task of the `nuage-vspk-negative-tests.yml` playbook.
```
TASK [nuage-vspk-negative-tests : Checking auth - missing username] **************************************************************************************************************************************************************************
task path: /Users/pdellaer/GitHub/nuagenetworks/vspk-ansible/roles/nuage-vspk-negative-tests/tasks/main.yml:1
Using module_utils file /Users/pdellaer/VirtualEnvs/vspk-ansible/lib/python2.7/site-packages/ansible/module_utils/basic.py
Using module_utils file /Users/pdellaer/VirtualEnvs/vspk-ansible/lib/python2.7/site-packages/ansible/module_utils/_text.py
Using module_utils file /Users/pdellaer/VirtualEnvs/vspk-ansible/lib/python2.7/site-packages/ansible/module_utils/pycompat24.py
Using module_utils file /Users/pdellaer/VirtualEnvs/vspk-ansible/lib/python2.7/site-packages/ansible/module_utils/six/__init__.py
Using module_utils file /Users/pdellaer/VirtualEnvs/vspk-ansible/lib/python2.7/site-packages/ansible/module_utils/six/_six.py
Using module file /Users/pdellaer/GitHub/nuagenetworks/vspk-ansible/library/nuage_vspk.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: pdellaer
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/pdellaer/.ansible/tmp/ansible-tmp-1495875609.06-82479027302570 `" && echo ansible-tmp-1495875609.06-82479027302570="` echo /Users/pdellaer/.ansible/tmp/ansible-tmp-1495875
609.06-82479027302570 `" ) && sleep 0'
<127.0.0.1> PUT /var/folders/7t/j48vp3pd2fzbz30yr8xjxpr00000gq/T/tmpsGRq3f TO /Users/pdellaer/.ansible/tmp/ansible-tmp-1495875609.06-82479027302570/nuage_vspk.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/pdellaer/.ansible/tmp/ansible-tmp-1495875609.06-82479027302570/ /Users/pdellaer/.ansible/tmp/ansible-tmp-1495875609.06-82479027302570/nuage_vspk.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/Users/pdellaer/VirtualEnvs/vspk-ansible/bin/python2.7 /Users/pdellaer/.ansible/tmp/ansible-tmp-1495875609.06-82479027302570/nuage_vspk.py; rm -rf "/Users/pdellaer/.ansible/tmp/ansible-tmp-1495875609.06-824790
27302570/" > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_args": {
            "auth": {
                "api_enterprise": "csp",
                "api_password": "csproot",
                "api_url": "https://localhost:8443",
                "api_version": "v4_0"
            },
            "command": "wait_for_job",
            "id": "dumy-job-uuid",
            "type": "Job"
        }
    },
    "msg": "missing required arguments: api_username"
}
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py
--- a/lib/ansible/module_utils/basic.py
+++ b/lib/ansible/module_utils/basic.py
@@ -1736,7 +1736,7 @@ def _check_argument_types(self, spec=None, param=None):
                     continue
                 wanted = 'str'
 
-            value = self.params[k]
+            value = param[k]
             if value is None:
                 continue
 


Test patch: