Repository: ansible/ansible

Issue:
ec2_ami is not deleting snapshots
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### 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-->
ec2_ami

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/centos/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, 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, BELOW THIS COMMENT, 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.-->
CentOS Linux release 7.4.1708 (Core)

##### SUMMARY
<!--- Explain the problem briefly -->
ec2_ami module does not appear to be deleting snapshots when using the "delete_snapshot=yes" parameter.

##### 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. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
ansible -m ec2_ami -a "delete_snapshot=yes state=absent wait=yes image_id=ami-0ee6716f" localhost
```

<!--- 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 response with non-empty "snapshots_deleted" list.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
localhost | SUCCESS => {
    "changed": true, 
    "invocation": {
        "module_args": {
            "architecture": "x86_64", 
            "aws_access_key": null, 
            "aws_secret_key": null, 
            "billing_products": null, 
            "delete_snapshot": true, 
            "description": "", 
            "device_mapping": null, 
            "ec2_url": null, 
            "enhanced_networking": null, 
            "image_id": "ami-0ee6716f", 
            "image_location": null, 
            "instance_id": null, 
            "kernel_id": null, 
            "launch_permissions": null, 
            "name": null, 
            "no_reboot": false, 
            "profile": null, 
            "purge_tags": false, 
            "ramdisk_id": null, 
            "region": null, 
            "root_device_name": null, 
            "security_token": null, 
            "sriov_net_support": null, 
            "state": "absent", 
            "tags": null, 
            "validate_certs": true, 
            "virtualization_type": "hvm", 
            "wait": true, 
            "wait_timeout": 900
        }
    }, 
    "msg": "AMI deregister operation complete.", 
    "snapshots_deleted": []
}
```

ec2_ami is not deleting snapshots
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### 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-->
ec2_ami

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/centos/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, 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, BELOW THIS COMMENT, 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.-->
CentOS Linux release 7.4.1708 (Core)

##### SUMMARY
<!--- Explain the problem briefly -->
ec2_ami module does not appear to be deleting snapshots when using the "delete_snapshot=yes" parameter.

##### 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. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
ansible -m ec2_ami -a "delete_snapshot=yes state=absent wait=yes image_id=ami-0ee6716f" localhost
```

<!--- 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 response with non-empty "snapshots_deleted" list.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
localhost | SUCCESS => {
    "changed": true, 
    "invocation": {
        "module_args": {
            "architecture": "x86_64", 
            "aws_access_key": null, 
            "aws_secret_key": null, 
            "billing_products": null, 
            "delete_snapshot": true, 
            "description": "", 
            "device_mapping": null, 
            "ec2_url": null, 
            "enhanced_networking": null, 
            "image_id": "ami-0ee6716f", 
            "image_location": null, 
            "instance_id": null, 
            "kernel_id": null, 
            "launch_permissions": null, 
            "name": null, 
            "no_reboot": false, 
            "profile": null, 
            "purge_tags": false, 
            "ramdisk_id": null, 
            "region": null, 
            "root_device_name": null, 
            "security_token": null, 
            "sriov_net_support": null, 
            "state": "absent", 
            "tags": null, 
            "validate_certs": true, 
            "virtualization_type": "hvm", 
            "wait": true, 
            "wait_timeout": 900
        }
    }, 
    "msg": "AMI deregister operation complete.", 
    "snapshots_deleted": []
}
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/amazon/ec2_ami.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/amazon/ec2_ami.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Constantin007 @Constantin07 @gunzy83 @scicoin-project @willthames @wilvk
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
The following code might need to be revised:

[](url)https://github.com/ansible/ansible/blob/e9270fb100f4d550888513b2af37b185d6e7d838/lib/ansible/modules/cloud/amazon/ec2_ami.py#L496-L501

BlockDeviceMappings data structure looks like:
```
"BlockDeviceMappings": [
    {
           "DeviceName": "/dev/sda1", 
           "Ebs": {
               "Encrypted": false, 
               "DeleteOnTermination": true, 
               "VolumeType": "gp2", 
               "VolumeSize": 8, 
               "SnapshotId": "snap-03d5c521dd94a89bb"
           }
   }
],
```

Note that SnapshotId is nested within Ebs.
Files identified in the description:
* [lib/ansible/modules/cloud/amazon/ec2_ami.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/amazon/ec2_ami.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Constantin007 @Constantin07 @gunzy83 @scicoin-project @willthames @wilvk
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
The following code might need to be revised:

[](url)https://github.com/ansible/ansible/blob/e9270fb100f4d550888513b2af37b185d6e7d838/lib/ansible/modules/cloud/amazon/ec2_ami.py#L496-L501

BlockDeviceMappings data structure looks like:
```
"BlockDeviceMappings": [
    {
           "DeviceName": "/dev/sda1", 
           "Ebs": {
               "Encrypted": false, 
               "DeleteOnTermination": true, 
               "VolumeType": "gp2", 
               "VolumeSize": 8, 
               "SnapshotId": "snap-03d5c521dd94a89bb"
           }
   }
],
```

Note that SnapshotId is nested within Ebs.

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/ec2_ami.py b/lib/ansible/modules/cloud/amazon/ec2_ami.py
--- a/lib/ansible/modules/cloud/amazon/ec2_ami.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_ami.py
@@ -496,7 +496,7 @@ def deregister_image(module, connection):
     snapshots = []
     if 'BlockDeviceMappings' in image:
         for mapping in image.get('BlockDeviceMappings'):
-            snapshot_id = mapping.get('SnapshotId')
+            snapshot_id = mapping.get('Ebs', {}).get('SnapshotId')
             if snapshot_id is not None:
                 snapshots.append(snapshot_id)
 


Test patch:
diff --git a/test/integration/targets/ec2_ami/tasks/main.yml b/test/integration/targets/ec2_ami/tasks/main.yml
--- a/test/integration/targets/ec2_ami/tasks/main.yml
+++ b/test/integration/targets/ec2_ami/tasks/main.yml
@@ -158,6 +158,7 @@
         that:
           - "result.changed"
           - "'image_id' not in result"
+          - "result.snapshots_deleted"
 
     # ============================================================