Skip to content

Instantly share code, notes, and snippets.

@plfort
Last active March 24, 2021 10:00
Show Gist options
  • Save plfort/eb53ab0e83676caf0d288b04084dd640 to your computer and use it in GitHub Desktop.
Save plfort/eb53ab0e83676caf0d288b04084dd640 to your computer and use it in GitHub Desktop.
Ansible role - Format and mount EBS NVME device for Ubuntu
#!/bin/bash
set -o errexit
set -o nounset
#https://github.com/oogali/ebs-automatic-nvme-mapping
for dev in /dev/nvme[0-9]*n1; do
devicename=$(nvme id-ctrl --output binary -v $dev |cut -c3073-3104 |tr -d '[:space:]')
if [ "$devicename" = "$1" ]; then
echo $dev
fi
done
- name: Create mount point
file:
dest: "{{ item.value.mount_point }}"
state: directory
owner: "{{ item.value.owner }}"
- name: "Get Nvme device for {{item.value.device_name}}"
ansible.builtin.shell:
cmd: "/usr/local/bin/devicename_nvme.sh {{item.value.device_name}}"
register: nvme_device
- name: Assert nvme device is found
assert:
that: nvme_device.stdout is match("^/dev/nvme\d+n1")
fail_msg: "Nvme device not found for {{item.value.device_name}}"
- name: Formatting the volume
filesystem:
dev: "{{nvme_device.stdout}}"
fstype: "{{item.value.fs}}"
- name: Mounting the filesystem
mount:
name: "{{item.value.mount_point}}"
src: "{{nvme_device.stdout}}"
fstype: "{{item.value.fs}}"
state: mounted
opts: "{{item.value.mount_opts|default('')}}"
- name: Install nvme-cli
apt:
package: nvme-cli
state: present
- name: Check for nvme devices
command: nvme list
register: nvme_exist
changed_when: False
check_mode: no
- name: Copy helper script to get Nvme device name switch device name set in AWS (/dev/sdX, ...)
copy:
src: devicename_nvme.sh
dest: /usr/local/bin/devicename_nvme.sh
mode: "u+rx"
- name: Format/mount each volume
include_tasks: ebs_format_mount.yml
with_dict: "{{ ebs_volumes }}"
- hosts: you hosts
become: yes
become_method: sudo
roles:
- role: aws-volumes
vars:
ebs_volumes:
volumeA:
device_name: /dev/sdf #defined when attaching the volume to the instance
mount_point: "/media/volumeA_mount_point"
fs: ext4
mount_opts: noatime,nodiratime
owner: ubuntu
volumeB:
device_name: /dev/sdg #defined when attaching the volume to the instance
mount_point: "/media/volumeB_mount_point"
fs: ext4
mount_opts: noatime,nodiratime
owner: ubuntu
Role structure:
aws-volume/
files/
devicename_nvme.sh
tasks/
main.yml
ebs_format_mount.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment