add make_lv role
This commit is contained in:
60
roles/make_lv/molecule/default/converge.yml
Normal file
60
roles/make_lv/molecule/default/converge.yml
Normal file
@@ -0,0 +1,60 @@
|
||||
---
|
||||
- name: Converge
|
||||
hosts: all
|
||||
pre_tasks:
|
||||
- name: Get /root/image.img stats
|
||||
ansible.builtin.stat:
|
||||
path: /root/image.img
|
||||
register: img_stat
|
||||
|
||||
- name: Create image file
|
||||
when: not img_stat.stat.exists
|
||||
ansible.builtin.command: dd if=/dev/zero of=/root/image.img bs=1M count=128
|
||||
register: dd_result
|
||||
changed_when: dd_result.rc == 0
|
||||
|
||||
- name: Get linked loop devices
|
||||
ansible.builtin.command: losetup -a
|
||||
changed_when: false
|
||||
register: loop_devs
|
||||
|
||||
- name: Link image to loop device
|
||||
when: "'/root/image.img' not in loop_devs.stdout"
|
||||
ansible.builtin.command: losetup --find --show -P /root/image.img
|
||||
register: loop_device
|
||||
changed_when: "'/dev/loop' in loop_device.stdout"
|
||||
|
||||
- name: Get all loop devices
|
||||
ansible.builtin.command: losetup -a
|
||||
register: losetup_out
|
||||
changed_when: false
|
||||
|
||||
- name: Find line with /root/image.img
|
||||
ansible.builtin.set_fact:
|
||||
matched_line: "{{ losetup_out.stdout_lines
|
||||
| select('search', '/root/image.img')
|
||||
| list
|
||||
| first
|
||||
| default('') }}"
|
||||
|
||||
- name: Extract loop device name (/dev/loopN)
|
||||
ansible.builtin.set_fact:
|
||||
loop_dev: "{{ matched_line | regex_search('(/dev/loop[0-9]+)', '\\1') | default('') }}"
|
||||
|
||||
- name: Debug result
|
||||
ansible.builtin.debug:
|
||||
msg: "/root/image.img is linked to {{ loop_dev[0] }}"
|
||||
when: loop_dev != ''
|
||||
|
||||
roles:
|
||||
- role: genlab.make_lv
|
||||
virtual_group: "group"
|
||||
logical_volume: "volume"
|
||||
lvm_dev: "{{ loop_dev[0] }}"
|
||||
size: 100%FREE
|
||||
fs_type: ext4
|
||||
storage_mountpoint: "/mnt"
|
||||
storage_mountpoint_mode: "0755"
|
||||
mountpoint_owner: root
|
||||
mountpoint_group: root
|
||||
is_container: true
|
||||
27
roles/make_lv/molecule/default/molecule.yml
Normal file
27
roles/make_lv/molecule/default/molecule.yml
Normal file
@@ -0,0 +1,27 @@
|
||||
---
|
||||
dependency:
|
||||
name: galaxy
|
||||
|
||||
driver:
|
||||
name: docker
|
||||
|
||||
platforms:
|
||||
- name: ubuntu
|
||||
image: geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2404}-ansible:latest
|
||||
pre_build_image: true
|
||||
command: ${MOLECULE_DOCKER_COMMAND:-""}
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
cgroupns_mode: host
|
||||
privileged: true
|
||||
|
||||
provisioner:
|
||||
name: ansible
|
||||
|
||||
verifier:
|
||||
name: ansible
|
||||
|
||||
lint: |
|
||||
set -e
|
||||
yamllint .
|
||||
ansible-lint .
|
||||
65
roles/make_lv/molecule/default/verify.yml
Normal file
65
roles/make_lv/molecule/default/verify.yml
Normal file
@@ -0,0 +1,65 @@
|
||||
---
|
||||
- name: Verify
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
any_errors_fatal: true
|
||||
vars:
|
||||
virtual_group: "group"
|
||||
logical_volume: "volume"
|
||||
lvm_dev: "/dev/loop0"
|
||||
fs_type: ext4
|
||||
storage_mountpoint: "/mnt"
|
||||
|
||||
tasks:
|
||||
- name: Get volume group list
|
||||
ansible.builtin.shell:
|
||||
cmd: "set -o pipefail ; pvs | grep {{ lvm_dev }}"
|
||||
executable: /bin/bash
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
register: vg_list
|
||||
|
||||
- name: Assert that volume group exists
|
||||
ansible.builtin.assert:
|
||||
that: virtual_group in vg_list.stdout
|
||||
success_msg: "{{ lvm_dev }} has {{ virtual_group }} group"
|
||||
fail_msg: "{{ lvm_dev }} DOES NOT have {{ virtual_group }} group"
|
||||
|
||||
- name: Get logical volume list
|
||||
ansible.builtin.shell:
|
||||
cmd: "set -o pipefail ; lvs | grep {{ virtual_group }} | grep {{ logical_volume }}"
|
||||
executable: /bin/bash
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
register: lv_list
|
||||
|
||||
- name: Assert that logical volume exists
|
||||
ansible.builtin.assert:
|
||||
that: logical_volume in lv_list.stdout
|
||||
success_msg: "{{ virtual_group }} has {{ logical_volume }} volume"
|
||||
fail_msg: "{{ virtual_group }} DOES NOT have {{ logical_volume }} volume"
|
||||
|
||||
- name: Get filesystem type
|
||||
ansible.builtin.command: "blkid /dev/mapper/{{ virtual_group }}-{{ logical_volume }} -o value -s TYPE"
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
register: current_fs
|
||||
|
||||
- name: Assert that current filesystem is the required one
|
||||
ansible.builtin.assert:
|
||||
that: current_fs.stdout == fs_type
|
||||
success_msg: "{{ virtual_group }} - {{ logical_volume }} has {{ fs_type }} filesystem"
|
||||
fail_msg: "{{ virtual_group }} - {{ logical_volume }} DOES NOT have {{ fs_type }} filesystem"
|
||||
|
||||
- name: Get logical volume mount info # noqa: command-instead-of-module — tells ansible-lint to ignore the error, because you can't actually
|
||||
# do it with ansible.builtin.mount as the linter begs me to do
|
||||
ansible.builtin.shell: "mount | grep -w '{{ virtual_group }}-{{ logical_volume }}'"
|
||||
register: mount_check
|
||||
ignore_errors: true
|
||||
changed_when: false
|
||||
|
||||
- name: Assert that volume is mounted
|
||||
ansible.builtin.assert:
|
||||
that: mount_check.rc == 0
|
||||
success_msg: "{{ virtual_group }}-{{ logical_volume }} is mounted at {{ storage_mountpoint }}"
|
||||
fail_msg: "{{ virtual_group }}-{{ logical_volume }} IS NOT mounted at {{ storage_mountpoint }}"
|
||||
Reference in New Issue
Block a user