Skip to content

Instantly share code, notes, and snippets.

@thesamet
Last active July 27, 2022 14:18
Show Gist options
  • Save thesamet/cae59663919f7e9a32755e4910e0fae4 to your computer and use it in GitHub Desktop.
Save thesamet/cae59663919f7e9a32755e4910e0fae4 to your computer and use it in GitHub Desktop.
Create raid0 LVM on EC2 ephemeral storage with Ansible.
---
# Create a RAID0 LVM through all ephemeral devices and mounts it.
- hosts: all
gather_facts: false
become: true
tasks:
- apt: name=lvm2 state=present
# On ubuntu, sometimes first ephemeral is already mounted in /mnt.
- mount: name=/mnt src=/dev/xvdb fstype=ext4 state=absent
- name: Loading list of devices
uri:
url: "http://169.254.169.254/2016-06-30/meta-data/block-device-mapping/"
return_content: yes
register: devices
- name: Load ephemeral device names
uri:
url: "http://169.254.169.254/2016-06-30/meta-data/block-device-mapping/{{item}}"
return_content: yes
with_items: "{{devices.content.split('\n')}}"
when: item.startswith('ephemeral')
register: device_names
- name: Create list of device names
set_fact:
ephs: "{{ device_names.results | selectattr('content', 'defined') | map(attribute='content') | map('regex_replace', '^sd', '/dev/xvd') | list }}"
- name: Create volume group
lvg: vg=vg.eph pvs="{{ephs | join(',')}}" state=present
- name: Create logical volume
lvol: vg=vg.eph lv=eph size=100%VG state=present opts="-i{{ephs | length}}"
- name: Format logical volume
filesystem: fstype=ext4 dev=/dev/vg.eph/eph
- name: Mount logical volume
mount: name=/mnt src=/dev/vg.eph/eph fstype=ext4 state=mounted
@parth995
Copy link

It's not an efficient solution I can guess. Line 14 doesn't return some of the volumes which are attached to EC2. Similar issue as reported here - https://stackoverflow.com/questions/49891037/retrieve-correct-amazon-attached-ebs-device-from-instance-metadata-endpoint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment