'How can I attach an existing EBS volume to an EC2 instance with Ansible?

I've looked through the Ansible documentation for ec2_instance and ec2_vol but both seem to only support creating a new EBS volume (either blank or from an EBS snapshot). I would like to attach an existing EBS volume directly to an instance, not create a snapshot of it and then create a new volume from that snapshot. Is there an Ansible module that does this or should I just use shell and run the right AWS CLI command?



Solution 1:[1]

You're looking for the union of the id: and instance: properties, as indicated by the language next to the id: property:

Volume id if you wish to attach an existing volume (requires instance) or remove an existing volume

- amazon.aws.ec2_vol:
    id: '{{ my_volume_id }}'
    instance: '{{ my_inst_id }}'
    device_name: /dev/xvdf # or whatever

Be aware (as this bit me) when they say instance: "None" to detach it, they really mean None, and not null or whatever else

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mdaniel