Skip to content

Instantly share code, notes, and snippets.

@sherwind
Created February 21, 2020 02:40
Show Gist options
  • Save sherwind/e0e1462aa9450593e7ab3cf8d958612f to your computer and use it in GitHub Desktop.
Save sherwind/e0e1462aa9450593e7ab3cf8d958612f to your computer and use it in GitHub Desktop.
Using Ansible to expand an EBS volume
- hosts: server
vars:
vol_new_size: 50
vol_drive: /dev/sdf
tasks:
- name: Gathering ec2 metada facts
ec2_metadata_facts:
- name: Get EBS volume ID and size
delegate_to: 127.0.0.1
become: no
ec2_vol:
region: "{{ ansible_ec2_placement_region }}"
instance: "{{ ansible_ec2_instance_id }}"
state: list
register: result
- set_fact:
vol_id: "{{ (result.volumes | selectattr('attachment_set.device', 'equalto', vol_drive) | list)[0].id }}"
vol_size: "{{ (result.volumes | selectattr('attachment_set.device', 'equalto', vol_drive) | list)[0].size }}"
when:
- not ansible_check_mode
- name: Expand EBS volume
delegate_to: 127.0.0.1
become: no
command: aws --region "{{ ansible_ec2_placement_region }}" ec2 modify-volume --volume-id "{{ vol_id }}" --size "{{ vol_new_size }}" --output json
changed_when: False
when:
- not ansible_check_mode
- vol_new_size > vol_size | int
- name: Wait for volume modification to complete
delegate_to: 127.0.0.1
become: no
command: aws --region "{{ ansible_ec2_placement_region }}" ec2 describe-volumes-modifications --volume-id "{{ vol_id }}" --output json
register: result
until: result.stdout.find('completed') != -1 or result.stdout.find('optimizing') != -1
retries: 60
delay: 5
changed_when: False
when:
- not ansible_check_mode
- vol_new_size > vol_size | int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment