Repository: ansible/ansible

Issue:
ec2_vpc_net multi_ok not working as expected
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report


##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
modules/cloud/amazon/ec2_vpc_net.py

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.0 (devel 64f98f855d) last updated 2017/02/02 00:15:21 (GMT +200)
  config file =
  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).
-->

##### 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.
-->
N/A

##### SUMMARY
<!--- Explain the problem briefly -->
Option "multi_ok" does not work properly allowing to provision a new vpc with cidr_block and tag Name already existent in the AWS account.

##### 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.
-->
Assuming a vpc with cidr_block 10.0.0.0/16 and tag Name 'test_vpc' already exists, run ansible with the module like this:


<!--- Paste example playbooks or commands between quotes below -->
```yaml
- ec2_vpc_net:
    name: test_vpc
    cidr_block: 10.0.0.0/16
    region: us-east-1
    multi_ok: true
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
A new vpc named 'test_vpc' with cidr_block 10.0.0.0/16 to be created, and the module to return changed = True.
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The ec2_vpc_net module return changed False and no new vpc is created for real.

<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/ec2_vpc_net.py b/lib/ansible/modules/cloud/amazon/ec2_vpc_net.py
--- a/lib/ansible/modules/cloud/amazon/ec2_vpc_net.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_vpc_net.py
@@ -118,9 +118,9 @@ def boto_exception(err):
     return error
 
 def vpc_exists(module, vpc, name, cidr_block, multi):
-    """Returns True or False in regards to the existence of a VPC. When supplied
+    """Returns None or a vpc object depending on the existence of a VPC. When supplied
     with a CIDR, it will check for matching tags to determine if it is a match
-    otherwise it will assume the VPC does not exist and thus return false.
+    otherwise it will assume the VPC does not exist and thus return None.
     """
     matched_vpc = None
 
@@ -130,11 +130,12 @@ def vpc_exists(module, vpc, name, cidr_block, multi):
         e_msg=boto_exception(e)
         module.fail_json(msg=e_msg)
 
-    if len(matching_vpcs) == 1:
+    if multi:
+        return None
+    elif len(matching_vpcs) == 1:
         matched_vpc = matching_vpcs[0]
     elif len(matching_vpcs) > 1:
-        if multi:
-            module.fail_json(msg='Currently there are %d VPCs that have the same name and '
+        module.fail_json(msg='Currently there are %d VPCs that have the same name and '
                              'CIDR block you specified. If you would like to create '
                              'the VPC anyway please pass True to the multi_ok param.' % len(matching_vpcs))
 


Test patch: