Skip to content

Fix block_devices to not rely on device path #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions ansible/roles/block_devices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ This is a convenience wrapper around the ansible modules:

It includes logic to handle OpenStack-provided volumes appropriately both for appliance instances and the Packer build VM.

To avoid issues with device names changing after e.g. reboots, devices are identified by serial number and mounted by filesystem UUID.

Requirements
------------

Expand All @@ -20,7 +22,7 @@ Role Variables
--------------

- `block_devices_partition_state`: Optional. Partition state, 'present' or 'absent' (as for parted) or 'skip'. Defaults to 'present'.
- `block_devices_device`: Required. Path to block device, e.g. '/dev/sda'. See `community.general.parted:device` and `community.general.filesystem:dev`.
- `block_devices_serial`: Required. Serial number of block device. For an OpenStack volume this is the volume ID.
- `block_devices_number`: Required. Partition number, e.g 1 for "/dev/sda1". See `community.general.parted:number`.
- `block_devices_fstype`: Required. Filesystem type, e.g.'ext4'. See `community.general.filesystem:fstype`
- `block_devices_resizefs`: Optional. Grow filesystem into block device space, 'yes' or 'no' (default). See `community.general.filesystem:resizefs` for applicable fileysystem types.
Expand Down Expand Up @@ -51,7 +53,7 @@ Example Playbook
The example variables below create an `ext4` partition on `/dev/sdb1` and mount it as `/mnt/files` with the default owner/group:

```yaml
block_devices_device: /dev/sdb
block_devices_serial: a1076455-da55-4e0c-bac8-ccc4698cff97
block_devices_number: 1
block_devices_fstype: ext4
block_devices_path: /mnt/files
Expand All @@ -61,25 +63,12 @@ This does the same:

```yaml
block_devices_configurations:
- device: /dev/sdb
- serial: a1076455-da55-4e0c-bac8-ccc4698cff97
number: 1
fstype: ext4
path: /mnt/files
```

This creates 'ext4' partitions on `/dev/sdb1` on `server` and `/dev/sdc1` on `server2`, both mounted at `/mnt/files`:

```yaml
block_devices_fstype: ext4
block_devices_path: /mnt/files
block_devices_number: 1
block_devices_configurations:
- device: /dev/sdb
hostnames: server1
- device: /dev/sdc
hostnames: server2
```

License
-------

Expand Down
44 changes: 44 additions & 0 deletions ansible/roles/block_devices/library/block_devices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/python

# Copyright: (c) 2021, StackHPC
# Apache 2 License

DOCUMENTATION = r'''
---
module: block_devices

short_description: Return block device paths by serial number.

options: (none)

author:
- Steve Brasier (@sjpb)
'''

RETURN = r'''
devices:
description: dict with device serial numbers as keys and full paths (e.g. /dev/sdb) as values
type: dict
return: always
'''

import json

from ansible.module_utils.basic import AnsibleModule

def run_module():
module_args = dict()
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
result = {"changed": False}
_, stdout, _ = module.run_command("lsblk --paths --json -O", check_rc=True)

device_info = json.loads(stdout)['blockdevices']
result['devices'] = dict((item['serial'], item['name']) for item in device_info)
module.exit_json(**result)

def main():
run_module()


if __name__ == '__main__':
main()
30 changes: 26 additions & 4 deletions ansible/roles/block_devices/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
- name: Enumerate block device paths by serial number
block_devices:
register: _block_devices

- name: Create partitions
parted:
device: "{{ item.get('device', block_devices_device) }}"
device: "{{ _device }}"
number: "{{ item.get('number', block_devices_number) }}"
state: "{{ item.get('partition_state', block_devices_partition_state) }}"
when: "item.get('partition_state', block_devices_partition_state) != 'skip'"
loop: "{{ block_devices_configurations }}"
vars:
_device: "{{ _block_devices.devices[ item.get('serial', block_devices_serial) ] }}"

- name: Create filesystems
filesystem:
fstype: "{{ item.get('fstype', block_devices_fstype) }}"
dev: "{{ item.get('device', block_devices_device) }}{{ item.get('number', block_devices_number) }}"
dev: "{{ _device }}{{ item.get('number', block_devices_number) }}"
resizefs: "{{ item.get('resizefs', block_devices_resizefs) }}"
state: "{{ item.get('filesystem_state', block_devices_filesystem_state) }}"
when: "item.get('filesystem_state', block_devices_filesystem_state) != 'skip'"
loop: "{{ block_devices_configurations }}"
vars:
_device: "{{ _block_devices.devices[ item.get('serial', block_devices_serial) ] }}"

- name: Get filesystem UUIDs
command:
cmd: "lsblk {{ _device }}{{ item.get('number', block_devices_number) }} --noheadings --output UUID"
loop: "{{ block_devices_configurations }}"
vars:
_device: "{{ _block_devices.devices[ item.get('serial', block_devices_serial) ] }}"
register: block_devices_uuids
changed_when: false
check_mode: no

- name: Ensure mount point exists
file:
path: "{{ item.get('path', block_devices_path) }}"
state: directory
loop: "{{ block_devices_configurations }}"

- name: Mount filesystems
- name: Mount filesystems by UUID
mount:
path: "{{ item.get('path', block_devices_path) }}"
src: "{{ item.get('device', block_devices_device) }}{{ item.get('number', block_devices_number) }}"
src: "UUID={{ _uuid }}"
fstype: "{{ item.get('fstype', block_devices_fstype) }}"
state: "{{ item.get('mount_state', block_devices_mount_state) }}"
vars:
_uuid: "{{ block_devices_uuids.results[block_devices_idx].stdout }}"
loop: "{{ block_devices_configurations }}"
loop_control:
index_var: block_devices_idx

- name: Set owner/group for mounted directory
file:
Expand Down