Skip to content

Instantly share code, notes, and snippets.

@ganto
Last active December 5, 2023 06:03
Show Gist options
  • Save ganto/ee6854a83305716e1c2c9ca57c8fe1fc to your computer and use it in GitHub Desktop.
Save ganto/ee6854a83305716e1c2c9ca57c8fe1fc to your computer and use it in GitHub Desktop.
Test playbook for Ansible lvol module
---
# This playbook will check a wide range of code paths of the Ansible lvol module.
#
# Make sure you adjust `test_vg` and `test_pvs` variables according to the test volume group.
#
# Invoke with: ansible-playbook [-K][-v] check-lvol.yml
#
- hosts: localhost
become: True
become_user: root
vars:
test_vg: 'vg_test'
test_pvs: '/dev/vdb'
tasks:
##
# First make sure we have a clean volume group
##
- name: Make sure test volume does not exist
lvol:
vg: "{{ test_vg }}"
lv: test
state: absent
force: yes
- name: Make sure test volume does not exist
lvol:
vg: "{{ test_vg }}"
lv: testmeta
state: absent
force: yes
- name: Make sure test snapshot does not exist
lvol:
vg: "{{ test_vg }}"
lv: testsnap
state: absent
force: yes
- name: Make sure test cache pool does not exist
lvol:
vg: "{{ test_vg }}"
lv: testcache
state: absent
force: yes
- name: Make sure test thin pool does not exist
lvol:
vg: "{{ test_vg }}"
lv: testpool
state: absent
force: yes
##
# Run basic tests
##
- name: Try to create a logical volume on invalid volume group
lvol:
vg: hqdbXe5AYh
lv: test
register: lvcreate
failed_when: not (lvcreate.msg is match("Volume group hqdbXe5AYh does not exist.")) or (lvcreate is success)
- name: Try to create a logical volume w/o size
lvol:
vg: "{{ test_vg }}"
lv: test
register: lvcreate
failed_when: not (lvcreate.msg is match("No size given.")) or (lvcreate is success)
- name: Check absence of logical volume
command: lvdisplay '{{ test_vg }}/test'
no_log: True
changed_when: False
register: lvdisplay
failed_when: lvdisplay.rc == 0
- name: Create a logical volume
lvol:
vg: "{{ test_vg }}"
lv: test
size: 512
- name: Check logical volume
command: lvdisplay '{{ test_vg }}/test'
no_log: True
changed_when: False
register: lvdisplay
- debug:
var: lvdisplay.stdout_lines
verbosity: 1
# ansible/ansible#23635
- name: Create a logical volume with full path
lvol:
vg: "{{ test_vg }}"
lv: "/dev/{{ test_vg }}/test"
size: 512
- name: Try to remove logical volume w/o force
lvol:
vg: "{{ test_vg }}"
lv: test
state: absent
register: lvremove
failed_when: not (lvremove.msg is match("Sorry, no removal of logical volume test without force=yes.")) or (lvremove is success)
- name: Remove logical volume
lvol:
vg: "{{ test_vg }}"
lv: test
state: absent
force: yes
- name: Check absence of logical volume
command: lvdisplay '{{ test_vg }}/test'
no_log: True
changed_when: False
register: lvdisplay
failed_when: lvdisplay.rc == 0
- name: Create cache volume
lvol:
vg: "{{ test_vg }}"
lv: testcache
size: 512
opts: --type cache-pool
- name: Check cache volume
shell: lvs --noheadings -o lv_attr '{{ test_vg }}/testcache' | sed 's/^ *//'
no_log: True
changed_when: False
register: lvattr
- debug:
var: lvattr.stdout_lines
verbosity: 1
failed_when: not (lvattr.stdout is match("C"))
- name: Remove cache volume
lvol:
vg: "{{ test_vg }}"
lv: testcache
state: absent
force: yes
- name: Try to create a logical volume with 101% size
lvol:
vg: "{{ test_vg }}"
lv: test
size: 101%
register: lvcreate
failed_when: not (lvcreate.msg is match("Size percentage cannot be larger than 100%")) or (lvcreate is success)
- name: Create a logical volume on given physical volume
lvol:
vg: "{{ test_vg }}"
lv: test
pvs: "{{ test_pvs }}"
size: 512
- name: Check logical volume
shell: lvs --noheadings -o devices '{{ test_vg }}/test' | sed 's/^ *//'
no_log: True
changed_when: False
register: lvdevices
- debug:
var: lvdevices.stdout_lines
verbosity: 1
failed_when: not (lvdevices.stdout is match(test_pvs))
- name: Resize logical volume
lvol:
vg: "{{ test_vg }}"
lv: test
size: 1024
- name: Check logical volume size
shell: lvs --noheadings -o lv_size '{{ test_vg }}/test' | sed 's/^ *//'
no_log: True
changed_when: False
register: lvsize
- debug:
var: lvsize.stdout_lines
verbosity: 1
failed_when: not (lvsize.stdout is match("1.00g"))
- name: Try to resize logical volume without shrinking
lvol:
vg: "{{ test_vg }}"
lv: test
size: 512
shrink: no
- name: Check logical volume size
shell: lvs --noheadings -o lv_size '{{ test_vg }}/test' | sed 's/^ *//'
no_log: True
changed_when: False
register: lvsize
- debug:
var: lvsize.stdout_lines
verbosity: 1
failed_when: not (lvsize.stdout is match("1.00g"))
- name: Resize the logical volume to % of volume group
lvol:
vg: "{{ test_vg }}"
lv: test
size: 5%VG
force: yes
- name: Try to shrink logical volume w/o force
lvol:
vg: "{{ test_vg }}"
lv: test
size: 512
register: lvresize
failed_when: not (lvresize.msg is match("Sorry, no shrinking of test without force=yes.")) or (lvresize is success)
- name: Try to shrink logical volume to size 0
lvol:
vg: "{{ test_vg }}"
lv: test
size: 0
register: lvresize
failed_when: not (lvresize.msg is match("Sorry, no shrinking of test to 0 permitted.")) or (lvresize is success)
- name: Try to shrink logical volume to size -1
lvol:
vg: "{{ test_vg }}"
lv: test
size: -1
register: lvresize
failed_when: not (lvresize.msg is match("Bad size specification of '-1'")) or (lvresize is success)
- name: Deactivate logical volume
lvol:
vg: "{{ test_vg }}"
lv: test
active: False
- name: Check logical volume attributes
shell: lvs --noheadings -o lv_attr '{{ test_vg }}/test' | sed 's/^ *//'
no_log: True
changed_when: False
register: lvattr
- debug:
var: lvattr.stdout_lines
verbosity: 1
failed_when: not (lvattr.stdout | search("i"))
- name: Activate logical volume
lvol:
vg: "{{ test_vg }}"
lv: test
active: True
- name: Check logical volume attributes
shell: lvs --noheadings -o lv_attr '{{ test_vg }}/test' | sed 's/^ *//'
no_log: True
changed_when: False
register: lvattr
- debug:
var: lvattr.stdout_lines
verbosity: 1
failed_when: not (lvattr.stdout is search("a"))
- name: Try to create a traditional snapshot w/o size
lvol:
vg: "{{ test_vg }}"
lv: test
snapshot: testsnap
register: lvcreate
failed_when: not (lvcreate.msg is match("No size given.")) or (lvcreate is success)
- name: Try to create a traditional snapshot of invalid LV
lvol:
vg: "{{ test_vg }}"
lv: NQRAkXIy23
snapshot: testsnap
register: lvcreate
failed_when: not (lvcreate.msg is match("Snapshot origin LV NQRAkXIy23 does not exist in volume group " + test_vg + ".")) or (lvcreate is success)
- name: Check absence of snapshot
command: lvdisplay '{{ test_vg }}/testsnap'
no_log: True
changed_when: False
register: lvdisplay
failed_when: lvdisplay.rc == 0
- name: Create a traditional snapshot
lvol:
vg: "{{ test_vg }}"
lv: test
snapshot: testsnap
size: 100
- name: Check snapshot attributes
shell: lvs --noheadings -o lv_attr '{{ test_vg }}/testsnap' | sed 's/^ *//'
no_log: True
changed_when: False
register: lvattr
- debug:
var: lvattr.stdout_lines
verbosity: 1
failed_when: not (lvattr.stdout | search("s"))
- name: Remove snapshot
lvol:
vg: "{{ test_vg }}"
lv: testsnap
state: absent
force: yes
- name: Check absence of snapshot
command: lvdisplay '{{ test_vg }}/testsnap'
no_log: True
changed_when: False
register: lvdisplay
failed_when: lvdisplay.rc == 0
- name: Remove logical volume
lvol:
vg: "{{ test_vg }}"
lv: test
state: absent
force: yes
##
# Test new functionality of ansible/ansible#19312
##
- name: Try to create a thin pool w/o size
lvol:
vg: "{{ test_vg }}"
thinpool: testpool
register: lvcreate
failed_when: not (lvcreate.msg is match("No size given.")) or (lvcreate is success)
- name: Check absence of thin pool
command: lvdisplay '{{ test_vg }}/testpool'
no_log: True
changed_when: False
register: lvdisplay
failed_when: lvdisplay.rc == 0
- name: Create a thin pool
lvol:
vg: "{{ test_vg }}"
thinpool: testpool
size: 512
- name: Check thin pool
command: lvdisplay '{{ test_vg }}/testpool'
no_log: True
changed_when: False
register: lvdisplay
- debug:
var: lvdisplay.stdout_lines
verbosity: 1
- name: Try to create thin volume on invalid thin pool
lvol:
vg: "{{ test_vg }}"
thinpool: G1SZOyebYp
lv: test
size: 100g
register: lvcreate
failed_when: not (lvcreate.msg is match("Thin pool LV G1SZOyebYp does not exist in volume group " + test_vg + ".")) or (lvcreate is success)
- name: Create thin volume
lvol:
vg: "{{ test_vg }}"
thinpool: testpool
lv: test
size: 100g
- name: Check logical volume
command: lvdisplay '{{ test_vg }}/test'
no_log: True
changed_when: False
register: lvdisplay
- debug:
var: lvdisplay.stdout_lines
verbosity: 1
- name: Resize thin pool
lvol:
vg: "{{ test_vg }}"
thinpool: testpool
size: 1024
- name: Check thin pool volume size
shell: lvs --noheadings -o lv_size '{{ test_vg }}/testpool' | sed 's/^ *//'
no_log: True
changed_when: False
register: lvsize
- debug:
var: lvsize.stdout_lines
verbosity: 1
failed_when: not (lvsize.stdout is match("1.00g"))
- name: Try to create snapshot of thin pool
lvol:
vg: "{{ test_vg }}"
thinpool: testpool
snapshot: testsnap
size: 100
register: lvcreate
failed_when: not (lvcreate.msg is match("Snapshots of thin pool LVs are not supported.")) or (lvcreate is success)
- name: Try to create snapshot of thin pool
lvol:
vg: "{{ test_vg }}"
lv: testpool
snapshot: testsnap
size: 100
register: lvcreate
failed_when: not (lvcreate.msg is match("Snapshots of thin pool LVs are not supported.")) or (lvcreate is success)
- name: Check absence of snapshot
command: lvdisplay '{{ test_vg }}/testsnap'
no_log: True
changed_when: False
register: lvdisplay
failed_when: lvdisplay.rc == 0
##
# Check ansible/ansible-modules-extra#2478
##
- name: Create a snapshot of thin volume
lvol:
vg: "{{ test_vg }}"
lv: test
snapshot: testsnap
- name: Check logical volume
command: lvdisplay '{{ test_vg }}/testsnap'
no_log: True
changed_when: False
register: lvdisplay
- debug:
var: lvdisplay.stdout_lines
verbosity: 1
- name: Remove snapshot
lvol:
vg: "{{ test_vg }}"
lv: testsnap
state: absent
force: yes
- name: Check absence of snapshot
command: lvdisplay '{{ test_vg }}/testsnap'
no_log: True
changed_when: False
register: lvdisplay
failed_when: lvdisplay.rc == 0
- name: Remove thin volume
lvol:
vg: "{{ test_vg }}"
lv: test
state: absent
force: yes
- name: Check absence of thin volume
command: lvdisplay '{{ test_vg }}/test'
no_log: True
changed_when: False
register: lvdisplay
failed_when: lvdisplay.rc == 0
- name: Try to remove thin pool w/o force
lvol:
vg: "{{ test_vg }}"
thinpool: testpool
state: absent
register: lvremove
failed_when: not (lvremove.msg is match("Sorry, no removal of logical volume testpool without force=yes.")) or (lvremove is success)
- name: Remove thin pool
lvol:
vg: "{{ test_vg }}"
thinpool: testpool
state: absent
force: yes
- name: Check absence of thin pool
command: lvdisplay '{{ test_vg }}/testpool'
no_log: True
changed_when: False
register: lvdisplay
failed_when: lvdisplay.rc == 0
##
# Check ansible/ansible-modules-extras#3042
##
- name: Create logical volume for pool
lvol:
vg: "{{ test_vg }}"
lv: "testpool"
size: 512
- name: Create logical volume for metadata
lvol:
vg: "{{ test_vg }}"
lv: "testmeta"
size: 8
- name: Prepare thinpool volume
command: lvconvert -y --zero n -c 512K --thinpool "{{ test_vg }}/testpool" --poolmetadata "{{ test_vg }}/testmeta"
- name: Check thinpool attributes
shell: lvs --noheadings -o seg_monitor '{{ test_vg }}/testpool' | sed 's/^ *//'
no_log: True
changed_when: False
register: segmon
- debug:
var: segmon.stdout_lines
verbosity: 1
failed_when: not (segmon.stdout is match("monitored"))
- name: Remove thinpool
lvol:
vg: "{{ test_vg }}"
lv: "testpool"
state: absent
force: yes
@alan-maskey
Copy link

So there is no way to check the status using lvol? instead we have use shell to check?

@ganto
Copy link
Author

ganto commented Dec 5, 2023

This was a playbook that I wrote to be a test suite for the Ansible lvol module. It's meant to test the module after doing Python code changes on the module itself to avoid regressions. If you write an Ansible playbook you don't need this.

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