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 (on host that executes module)

  • python >= 2.6
  • boto

Options

parameter required default choices comments
aws_access_key
no
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
no
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)
no no
  • yes
  • no
When set to "yes", the volume will be deleted upon instance termination.
device_name
no
device id to override device mapping. Assumes /dev/sdf for Linux/UNIX and /dev/xvdf for Windows.
ec2_url
no
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)
no
Enable encryption at rest for this volume.
id
(added in 1.6)
no
volume id if you wish to attach an existing volume (requires instance) or remove an existing volume
instance
no
instance ID if you wish to attach the volume. Since 1.9 you can set to None to detach.
iops
(added in 1.3)
no 100
the provisioned IOPs you want to associate with this volume (integer).
kms_key_id
(added in 2.3)
no
Specify the id of the KMS key to use.
name
(added in 1.6)
no
volume Name tag if you wish to attach an existing volume (requires instance)
profile
(added in 1.6)
no
Uses a boto profile. Only works with boto >= 2.24.0.
region
no
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)
no
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)
no
snapshot ID on which to base the volume
state
(added in 1.6)
no present
  • 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)
no
tag:value pairs to add to the volume after creation
validate_certs
(added in 1.5)
no yes
  • yes
  • no
When set to "no", SSL certificates will not be validated for boto versions >= 2.6.0.
volume_size
no
size of volume (in GB) to create.
volume_type
(added in 1.9)
no 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
no
zone in which to create the volume, if unset uses the zone the instance is in (if set)
aliases: aws_zone, ec2_zone

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 Return Values, the following are the fields unique to this module:

name description returned type sample
device
device name of attached volume
when success string /def/sdf
volume
a dictionary containing detailed attributes of the volume
when success string {'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
the id of volume
when success string vol-35b333d9
volume_type
the volume type
when success string standard

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

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.

Maintenance Info

For more information about Red Hat’s this support of this module, please refer to this knowledge base article<https://access.redhat.com/articles/rhel-top-support-policies>

For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Testing Ansible and Developing Modules.

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