Repository: ansible/ansible

Issue:
ec2_vol: Not all volumes configure deleteOnTermination properly
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
ec2_vol

##### ANSIBLE VERSION
```
$ ansible --version
ansible 2.2.0.0
  config file = /Users/johnr/src/mycompany/ansible-infrastructure/ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
```
[defaults]
roles_path = roles/:galaxy_roles/
nocows=0
inventory = ./inventory
ansible_python_interpreter=/usr/local/bin/python
vault_password_file = /etc/ansible/.vault_pass
host_key_checking=False
retry_files_save_path = ~/.ansible-retry
ansible_managed = This file is managed by Ansible. Do NOT edit this file manually!
become = true

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o ForwardAgent=yes -o UserKnownHostsFile=/dev/null
control_path = %(directory)s/%%h-%%p-%%r
```

##### OS / ENVIRONMENT
Running Ansible from OSX El Capitan

##### SUMMARY
I've noticed that when attaching ec2_vol to a number of hosts, some of them don't set the deleteOnTermination properly. For example:

When running the following play:
```
- name: Attach additional volume for /var/lib/docker
  ec2_vol:
    instance: '{{ ec2_id }}'
    volume_size: '{{ ose_storage_lib_docker_size }}'
    volume_type: gp2
    delete_on_termination: yes
    device_name: /dev/xvdc
    region: '{{ ec2_region }}'
```

The 4th, 5th, and last host ` "deleteOnTermination": "false"`
```
TASK [myrole.openshift-docker-storage : Attach additional volume for /var/lib/docker] ***
task path: /Users/johnr/src/mycompany/ansible-infrastructure/roles/myrole.openshift-docker-storage/tasks/ec2_attach_volumes.yml:11
changed: [54.161.219.249] => {"changed": true, "device": "/dev/xvdc", "volume": {"attachment_set": {"attach_time": "2017-03-11T21:23:19.000Z", "deleteOnTermination": "true", "device": "/dev/xvdc", "instance_id": "i-088c6970fba9f5ba5", "status": "attached"}, "create_time": "2017-03-11T21:23:15.559Z", "encrypted": false, "id": "vol-0913418e0beb98a2a", "iops": 150, "size": 50, "snapshot_id": "", "status": "in-use", "tags": {}, "type": "gp2", "zone": "us-east-1b"}, "volume_id": "vol-0913418e0beb98a2a", "volume_type": "gp2"}
changed: [184.72.120.193] => {"changed": true, "device": "/dev/xvdc", "volume": {"attachment_set": {"attach_time": "2017-03-11T21:23:19.000Z", "deleteOnTermination": "true", "device": "/dev/xvdc", "instance_id": "i-0667953fb4f7882cb", "status": "attached"}, "create_time": "2017-03-11T21:23:15.580Z", "encrypted": false, "id": "vol-02b045f704496086c", "iops": 150, "size": 50, "snapshot_id": "", "status": "in-use", "tags": {}, "type": "gp2", "zone": "us-east-1b"}, "volume_id": "vol-02b045f704496086c", "volume_type": "gp2"}
changed: [54.144.72.183] => {"changed": true, "device": "/dev/xvdc", "volume": {"attachment_set": {"attach_time": "2017-03-11T21:23:19.000Z", "deleteOnTermination": "true", "device": "/dev/xvdc", "instance_id": "i-00e992c4582b3ee67", "status": "attached"}, "create_time": "2017-03-11T21:23:15.550Z", "encrypted": false, "id": "vol-0acac20704aaef4f2", "iops": 150, "size": 50, "snapshot_id": "", "status": "in-use", "tags": {}, "type": "gp2", "zone": "us-east-1b"}, "volume_id": "vol-0acac20704aaef4f2", "volume_type": "gp2"}
changed: [52.90.87.38] => {"changed": true, "device": "/dev/xvdc", "volume": {"attachment_set": {"attach_time": "2017-03-11T21:23:22.000Z", "deleteOnTermination": "false", "device": "/dev/xvdc", "instance_id": "i-0415b64dd3b624c72", "status": "attached"}, "create_time": "2017-03-11T21:23:15.580Z", "encrypted": false, "id": "vol-00c1cca510971b626", "iops": 150, "size": 50, "snapshot_id": "", "status": "in-use", "tags": {}, "type": "gp2", "zone": "us-east-1b"}, "volume_id": "vol-00c1cca510971b626", "volume_type": "gp2"}
changed: [54.89.147.61] => {"changed": true, "device": "/dev/xvdc", "volume": {"attachment_set": {"attach_time": "2017-03-11T21:23:19.000Z", "deleteOnTermination": "false", "device": "/dev/xvdc", "instance_id": "i-03f7d0b0264f5f354", "status": "attached"}, "create_time": "2017-03-11T21:23:15.523Z", "encrypted": false, "id": "vol-09574252c1d839057", "iops": 150, "size": 50, "snapshot_id": "", "status": "in-use", "tags": {}, "type": "gp2", "zone": "us-east-1b"}, "volume_id": "vol-09574252c1d839057", "volume_type": "gp2"}
changed: [52.23.253.150] => {"changed": true, "device": "/dev/xvdc", "volume": {"attachment_set": {"attach_time": "2017-03-11T21:23:30.000Z", "deleteOnTermination": "true", "device": "/dev/xvdc", "instance_id": "i-043b18fe857ac1901", "status": "attached"}, "create_time": "2017-03-11T21:23:27.154Z", "encrypted": false, "id": "vol-0313f724a65ddc965", "iops": 150, "size": 50, "snapshot_id": "", "status": "in-use", "tags": {}, "type": "gp2", "zone": "us-east-1b"}, "volume_id": "vol-0313f724a65ddc965", "volume_type": "gp2"}
changed: [52.202.8.217] => {"changed": true, "device": "/dev/xvdc", "volume": {"attachment_set": {"attach_time": "2017-03-11T21:23:30.000Z", "deleteOnTermination": "true", "device": "/dev/xvdc", "instance_id": "i-0a1de14de76312511", "status": "attached"}, "create_time": "2017-03-11T21:23:27.309Z", "encrypted": false, "id": "vol-06b9ed22d37e767af", "iops": 150, "size": 50, "snapshot_id": "", "status": "in-use", "tags": {}, "type": "gp2", "zone": "us-east-1b"}, "volume_id": "vol-06b9ed22d37e767af", "volume_type": "gp2"}
changed: [54.243.10.91] => {"changed": true, "device": "/dev/xvdc", "volume": {"attachment_set": {"attach_time": "2017-03-11T21:23:32.000Z", "deleteOnTermination": "false", "device": "/dev/xvdc", "instance_id": "i-0e8e866133a150516", "status": "attached"}, "create_time": "2017-03-11T21:23:28.841Z", "encrypted": false, "id": "vol-045623db789d1afb0", "iops": 150, "size": 50, "snapshot_id": "", "status": "in-use", "tags": {}, "type": "gp2", "zone": "us-east-1b"}, "volume_id": "vol-045623db789d1afb0", "volume_type": "gp2"}
```

##### STEPS TO REPRODUCE
See above

##### EXPECTED RESULTS
I would expect that all the nodes set deleteOnTermination to true as I defined it in yaml


Discussion:
This may be relevant too:
```
johnr@spintastic-johnr:ansible-infrastructure (master) $ pip list | grep boto
boto (2.36.0)
boto3 (1.4.0)
botocore (1.4.38)
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
```
Ok, quick update. I did some spot checking and it does appear that the ec2_vol is setting delete on termination as I expect. Its just misleading in the ansible output and should probably be fixed to avoid confusion.

<img width="1118" alt="screen shot 2017-03-11 at 4 39 11 pm" src="https://cloud.githubusercontent.com/assets/7714111/23827079/657221f8-0679-11e7-92b8-e8449f6afc63.png">

cc @lwade
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/ec2_vol.py b/lib/ansible/modules/cloud/amazon/ec2_vol.py
--- a/lib/ansible/modules/cloud/amazon/ec2_vol.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_vol.py
@@ -621,6 +621,15 @@ def main():
 
         # Add device, volume_id and volume_type parameters separately to maintain backward compatibility
         volume_info = get_volume_info(volume, state)
+
+        # deleteOnTermination is not correctly reflected on attachment
+        if module.params.get('delete_on_termination'):
+            for attempt in range(0, 8):
+                if volume_info['attachment_set'].get('deleteOnTermination') == 'true':
+                    break
+                time.sleep(5)
+                volume = ec2.get_all_volumes(volume_ids=volume.id)[0]
+                volume_info = get_volume_info(volume, state)
         module.exit_json(changed=changed, volume=volume_info, device=volume_info['attachment_set']['device'],
                          volume_id=volume_info['id'], volume_type=volume_info['type'])
     elif state == 'absent':


Test patch: