ec2_vol - create and attach a volume, return volume id and device map

Synopsis

  • creates an EBS volume and optionally attaches it to an instance. If both an instance ID and a device name is given and the instance has a device at the device name, then no volume is created and no attachment is made. This module has a dependency on python-boto.

Requirements

The below requirements are needed on the host that executes this module.

  • python >= 2.6
  • boto

Parameters

Parameter Choices/Defaults Comments
aws_access_key Default:
None
AWS access key. If not set then the value of the AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY or EC2_ACCESS_KEY environment variable is used.

aliases: ec2_access_key, access_key
aws_secret_key Default:
None
AWS secret key. If not set then the value of the AWS_SECRET_ACCESS_KEY, AWS_SECRET_KEY, or EC2_SECRET_KEY environment variable is used.

aliases: ec2_secret_key, secret_key
delete_on_termination
(added in 2.1)
    Choices:
  • yes
  • no
When set to "yes", the volume will be deleted upon instance termination.
device_name Default:
None
device id to override device mapping. Assumes /dev/sdf for Linux/UNIX and /dev/xvdf for Windows.
ec2_url Default:
None
Url to use to connect to EC2 or your Eucalyptus cloud (by default the module will use EC2 endpoints). Ignored for modules where region is required. Must be specified for all other modules if region is not used. If not set then the value of the EC2_URL environment variable, if any, is used.
encrypted
(added in 1.8)
Default:
no
Enable encryption at rest for this volume.
id
(added in 1.6)
Default:
None
volume id if you wish to attach an existing volume (requires instance) or remove an existing volume
instance Default:
None
instance ID if you wish to attach the volume. Since 1.9 you can set to None to detach.
iops
(added in 1.3)
Default:
100
the provisioned IOPs you want to associate with this volume (integer).
kms_key_id
(added in 2.3)
Default:
None
Specify the id of the KMS key to use.
name
(added in 1.6)
Default:
None
volume Name tag if you wish to attach an existing volume (requires instance)
profile
(added in 1.6)
Default:
None
Uses a boto profile. Only works with boto >= 2.24.0.
region
The AWS region to use. If not specified then the value of the AWS_REGION or EC2_REGION environment variable, if any, is used. See http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region

aliases: aws_region, ec2_region
security_token
(added in 1.6)
Default:
None
AWS STS security token. If not set then the value of the AWS_SECURITY_TOKEN or EC2_SECURITY_TOKEN environment variable is used.

aliases: access_token
snapshot
(added in 1.5)
Default:
None
snapshot ID on which to base the volume
state
(added in 1.6)
    Choices:
  • absent
  • present
  • list
whether to ensure the volume is present or absent, or to list existing volumes (The list option was added in version 1.8).
tags
(added in 2.3)
Default:
{}
tag:value pairs to add to the volume after creation
validate_certs
(added in 1.5)
    Choices:
  • no
  • yes
When set to "no", SSL certificates will not be validated for boto versions >= 2.6.0.
volume_size Default:
None
size of volume (in GB) to create.
volume_type
(added in 1.9)
Default:
standard
Type of EBS volume; standard (magnetic), gp2 (SSD), io1 (Provisioned IOPS), st1 (Throughput Optimized HDD), sc1 (Cold HDD). "Standard" is the old EBS default and continues to remain the Ansible default for backwards compatibility.
zone Default:
None
zone in which to create the volume, if unset uses the zone the instance is in (if set)

aliases: aws_zone, ec2_zone

Notes

Note

  • If parameters are not set within the module, the following environment variables can be used in decreasing order of precedence AWS_URL or EC2_URL, AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY or EC2_ACCESS_KEY, AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY or EC2_SECRET_KEY, AWS_SECURITY_TOKEN or EC2_SECURITY_TOKEN, AWS_REGION or EC2_REGION
  • Ansible uses the boto configuration file (typically ~/.boto) if no credentials are provided. See http://boto.readthedocs.org/en/latest/boto_config_tut.html
  • AWS_REGION or EC2_REGION can be typically be used to specify the AWS region, when required, but this can also be configured in the boto config file

Examples

# Simple attachment action
- ec2_vol:
    instance: XXXXXX
    volume_size: 5
    device_name: sdd

# Example using custom iops params
- ec2_vol:
    instance: XXXXXX
    volume_size: 5
    iops: 100
    device_name: sdd

# Example using snapshot id
- ec2_vol:
    instance: XXXXXX
    snapshot: "{{ snapshot }}"

# Playbook example combined with instance launch
- ec2:
    keypair: "{{ keypair }}"
    image: "{{ image }}"
    wait: yes
    count: 3
  register: ec2
- ec2_vol:
    instance: "{{ item.id }}"
    volume_size: 5
  with_items: "{{ ec2.instances }}"
  register: ec2_vol

# Example: Launch an instance and then add a volume if not already attached
#   * Volume will be created with the given name if not already created.
#   * Nothing will happen if the volume is already attached.
#   * Requires Ansible 2.0

- ec2:
    keypair: "{{ keypair }}"
    image: "{{ image }}"
    zone: YYYYYY
    id: my_instance
    wait: yes
    count: 1
  register: ec2

- ec2_vol:
    instance: "{{ item.id }}"
    name: my_existing_volume_Name_tag
    device_name: /dev/xvdf
  with_items: "{{ ec2.instances }}"
  register: ec2_vol

# Remove a volume
- ec2_vol:
    id: vol-XXXXXXXX
    state: absent

# Detach a volume (since 1.9)
- ec2_vol:
    id: vol-XXXXXXXX
    instance: None

# List volumes for an instance
- ec2_vol:
    instance: i-XXXXXX
    state: list

# Create new volume using SSD storage
- ec2_vol:
    instance: XXXXXX
    volume_size: 50
    volume_type: gp2
    device_name: /dev/xvdf

# Attach an existing volume to instance. The volume will be deleted upon instance termination.
- ec2_vol:
    instance: XXXXXX
    id: XXXXXX
    device_name: /dev/sdf
    delete_on_termination: yes

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key Returned Description
device
string
when success
device name of attached volume

Sample:
/def/sdf
volume
string
when success
a dictionary containing detailed attributes of the volume

Sample:
{'status': 'in-use', 'zone': 'us-east-1b', 'tags': {'env': 'dev'}, 'encrypted': False, 'iops': None, 'create_time': '2015-10-21T14:36:08.870Z', 'snapshot_id': '', 'attachment_set': {'device': '/dev/sdf', 'instance_id': 'i-8356263c', 'deleteOnTermination': 'false', 'status': 'attached', 'attach_time': '2015-10-23T00:22:29.000Z'}, 'type': 'standard', 'id': 'vol-35b333d9', 'size': 1}
volume_id
string
when success
the id of volume

Sample:
vol-35b333d9
volume_type
string
when success
the volume type

Sample:
standard


Status

This module is flagged as stableinterface which means that the maintainers for this module guarantee that no backward incompatible interface changes will be made.

Support

For more information about Red Hat’s support of this module, please refer to this Knowledge Base article

Author

  • Lester Wade (@lwade)

Hint

If you notice any issues in this documentation you can edit this document to improve it.

© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.5/modules/ec2_vol_module.html