Repository: ansible/ansible

Issue:
'azure_rm_subnet' with 'state: absent' fails when subnet was already not existing
From @techraf on 2016-10-10T13:16:21Z
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

azure_rm_subnet
##### ANSIBLE VERSION

```
ansible 2.1.2.0
  config file = /home/techraf/devops/infra-azure/ansible.cfg
  configured module search path = Default w/o overrides
```
##### CONFIGURATION
##### OS / ENVIRONMENT

Debian Jessie
##### SUMMARY

`azure_rm_subnet` requires the parameter `virtual_network_name` to be provided, otherwise:

```
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "missing required arguments: virtual_network_name"}
```

**But** running the module with `state: absent` fails when the virtual network specified in `virtual_network_name` is non-existent.
##### STEPS TO REPRODUCE

Delete the virtual_network then run the `azure_rm_subnet` task with `state: absent`.

```
- name: Ensure subnet does not exist
  azure_rm_subnet:
    resource_group: Testing
    name: subnet001
    state: absent
    virtual_network_name: testvn001
```
##### EXPECTED RESULTS

```
ok: [localhost]
```
##### ACTUAL RESULTS

```
TASK [Ensure subnet does not exist] ********************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Error deleting subnet subnet001 - The Resource 'Microsoft.Network/virtualNetworks/testvn001' under resource group 'Testing' was not found."}
```

Copied from original issue: ansible/ansible-modules-core#5211

'azure_rm_subnet' with 'state: absent' fails when subnet was already not existing
From @techraf on 2016-10-10T13:16:21Z
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

azure_rm_subnet
##### ANSIBLE VERSION

```
ansible 2.1.2.0
  config file = /home/techraf/devops/infra-azure/ansible.cfg
  configured module search path = Default w/o overrides
```
##### CONFIGURATION
##### OS / ENVIRONMENT

Debian Jessie
##### SUMMARY

`azure_rm_subnet` requires the parameter `virtual_network_name` to be provided, otherwise:

```
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "missing required arguments: virtual_network_name"}
```

**But** running the module with `state: absent` fails when the virtual network specified in `virtual_network_name` is non-existent.
##### STEPS TO REPRODUCE

Delete the virtual_network then run the `azure_rm_subnet` task with `state: absent`.

```
- name: Ensure subnet does not exist
  azure_rm_subnet:
    resource_group: Testing
    name: subnet001
    state: absent
    virtual_network_name: testvn001
```
##### EXPECTED RESULTS

```
ok: [localhost]
```
##### ACTUAL RESULTS

```
TASK [Ensure subnet does not exist] ********************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Error deleting subnet subnet001 - The Resource 'Microsoft.Network/virtualNetworks/testvn001' under resource group 'Testing' was not found."}
```

Copied from original issue: ansible/ansible-modules-core#5211



Discussion:
From @ansibot on 2016-10-10T13:16:21Z

<!-- @techraf: Thanks for filing this bug report! -->

@ansible 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 --->

cc @haroldwongms @nitzmahone @tstringer
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @chouseknecht
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @xscript @yaweiw @zikalino
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @yuwzho
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @trstringer
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Thanks for your reporting, I will fix it soon!
From @ansibot on 2016-10-10T13:16:21Z

<!-- @techraf: Thanks for filing this bug report! -->

@ansible 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 --->

cc @haroldwongms @nitzmahone @tstringer
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @chouseknecht
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @xscript @yaweiw @zikalino
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @yuwzho
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @trstringer
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Thanks for your reporting, I will fix it soon!

Resolving patch:
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_subnet.py b/lib/ansible/modules/cloud/azure/azure_rm_subnet.py
--- a/lib/ansible/modules/cloud/azure/azure_rm_subnet.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_subnet.py
@@ -254,7 +254,7 @@ def exec_module(self, **kwargs):
                                                                                                  resource_guid=nsg.resource_guid)
 
                 self.results['state'] = self.create_or_update_subnet(subnet)
-            elif self.state == 'absent':
+            elif self.state == 'absent' and changed:
                 # delete subnet
                 self.delete_subnet()
                 # the delete does not actually return anything. if no exception, then we'll assume


Test patch:
diff --git a/test/integration/targets/azure_rm_subnet/tasks/main.yml b/test/integration/targets/azure_rm_subnet/tasks/main.yml
--- a/test/integration/targets/azure_rm_subnet/tasks/main.yml
+++ b/test/integration/targets/azure_rm_subnet/tasks/main.yml
@@ -82,6 +82,17 @@
     virtual_network_name: My_Virtual_Network
     resource_group: "{{ resource_group }}"
 
+- name: Remove subnet (idempotent)
+  azure_rm_subnet:
+    state: absent
+    name: foobar
+    virtual_network_name: My_Virtual_Network
+    resource_group: "{{ resource_group }}"
+  register: output
+
+- assert:
+    that: not output.changed
+
 - name: Remove security group
   azure_rm_securitygroup:
     resource_group: "{{ resource_group }}"