Commit 497d0173 by Max Rothman Committed by Kevin Falcone

OPS-1385 role for mounting/formatting volumes

This implements a role that consumes a volume list of dicts and ensures
those are all mounted in the expected location on your system as well as
formatting any drives with the wrong fstype.

As a safety feature, it defaults to being unwilling to unmount drives
(since that might result in data loss or mismanagement) and there is a
flag you can -e to enable it.

It is assumed you will run this role passing along your volume list from
your main playbook and secure vars.
parent bf109aae
# Expects a list of dicts with these keys
# - device: /dev/xvdk
# mount: /edx/var/mongo
# options: "defaults,noatime"
# fstype: ext4
# While mount, options and fstype are pretty standard in our app, the device names
# will be highly dependent on how you stand up your instances.
#
# Additionally - order is important if you have child directories. If you want to mount
# /edx/var/mongo and /edx/var/mongo/mongodb/journal, you must specify them in that order,
# otherwise this role will mount /edx/var/mongo over the top of /edx/var/mongo/mongodb/journal
# which is not what you wanted.
volumes: []
UNMOUNT_DISKS: false
#
# edX Configuration
#
# github: https://github.com/edx/configuration
# wiki: https://github.com/edx/configuration/wiki
# code style: https://github.com/edx/configuration/wiki/Ansible-Coding-Conventions
# license: https://github.com/edx/configuration/blob/master/LICENSE.TXT
#
#
#
# Tasks for role mount_dbs
#
# Overview:
#
# This role ensures that the correct EBS volumes are mounted to the right locations.
# If the volumes are already mounted to the correct place, this role does nothing.
# This task will be skipped if UNMOUNT_DISKS is false, causing the next task
# to error if the disk has the wrong fstype but is already mounted
- name: Unmount disk if fstype is wrong
mount:
name: "{{ (ansible_mounts | selectattr('device', 'equalto', item.device) | first | default({'mount': None})).mount }}"
src: "{{ (ansible_mounts | selectattr('device', 'equalto', item.device) | first | default({'device': None})).device }}"
fstype: "{{ (ansible_mounts | selectattr('device', 'equalto', item.device) | first | default({'fstype': None})).fstype }}"
state: unmounted
when: "{{ UNMOUNT_DISKS and (ansible_mounts | selectattr('device', 'equalto', item.device) | first | default({'fstype': None})).fstype != item.fstype }}"
with_items: volumes
# Noop & reports "ok" if fstype is correct
# Errors if fstype is wrong and disk is mounted (hence above task)
- name: Create filesystem
filesystem:
dev: "{{ item.device }}"
fstype: "{{ item.fstype }}"
# Necessary because AWS gives some ephemeral disks the wrong fstype by default
force: true
with_items: volumes
# This can fail if one volume is mounted on a child directory as another volume
# and it attempts to unmount the parent first. This is generally fixable by rerunning.
# Order is super dependent here, but we're iterating ansible_mounts (in order to identify
# all current mounts in the system) not volumes, which would be reversible.
# Possibly fixable by saving this list of unmounts off and comparing it to volumes, but this
# task rarely runs, since on server setup, the disks are unmounted, and in we won't
# be unmounting disks unless you set UNMOUNT_DISKS to true.
- name: Unmount disks mounted to the wrong place
mount:
name: "{{ item.mount }}"
src: "{{ item.device }}"
fstype: "{{ item.fstype }}"
state: unmounted
when: >
UNMOUNT_DISKS and
volumes | selectattr('device', 'equalto', item.device) | list | length != 0 and
(volumes | selectattr('device', 'equalto', item.device) | first).mount != item.mount
with_items: ansible_mounts
# fail here if you attempted to unmount something instead of unmounting it
# Meaning UNMOUNT disks is false, but the volumes indicates we should be fiddling mounts
- name: Mount disks
mount:
name: "{{ item.mount }}"
src: "{{ item.device }}"
state: mounted
fstype: "{{ item.fstype }}"
opts: "{{ item.options }}"
with_items: volumes
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment