In this lesson, you will learn how to use the Ansible lvol module & other Ansible storage-related modules in Linux such as; parted, lvg, lvol, filesystem, mount, etc.
Contents
Using The Ansible parted Module With Examples
The parted module is used to partition block devices. There are several options that can be used with the parted module. As I always mention in the previous lessons, you can use the “ansible-doc <module>” command to check the manual page of a module for more information about the module.
Let’s see how this module can be used with examples by creating a partition of 1 GB from the sdb block device.
*create the playbook
[lisa@drsdev1 ~]$ cd ansible/
[lisa@drsdev1 ansible]$ vi storage.yml
---
- name: configure storage
hosts: hqsdev1.tekneed.com
tasks:
- name: partition sdb to 1gb
parted:
device: /dev/sdb
number: 1
state: present
part_end: 1GB
The playbook above will use the parted module to partition the device, sdb, on hqsdev1.tekneed.com as the first device. It will be partitioned with 1gb storage size with the use of the part_end option.
Please note that 1GB is different from 1GiB. If GiB is used, you will have the exact partition size as compared to GB.
GB is used in this example, while GiB will be used in the next example below while creating a volume group so that you can notice the difference.
*Do a playbook syntax check.
[lisa@drsdev1 ansible]$ ansible-playbook storage.yml --syntax-check
playbook: storage.ym
*Run the playbook
[lisa@drsdev1 ansible]$ ansible-playbook storage.yml
PLAY [configure storage] *******************************************************
..................
*verify that sdb1 has been created on the managed host.
Using The Ansible lvg module With Examples
The Ansible lvg module is used to manage volume group (VG); such as creating, removing, and resizing volume groups, etc.
Let’s see how this module can be used with examples.
*create a playbook
---
- name: configure storage
hosts: hqsdev1.tekneed.com
tasks:
- name: partition sdb to 5gb
parted:
device: /dev/sdb
number: 1
state: present
part_end: 5GiB
- name: create a VG on sdb1
lvg:
vg: tekneed-vg
pvs: /dev/sdb1
pesize: 32
The playbook above will partition the sdb block device with a storage size of 5gb. Like I mentioned while creating the first playbook above, GB is different from GiB.
The volume group, tekneed-vg will be created from sdb1 with a physical extent size of 32
*Do a playbook syntax check
[lisa@drsdev1 ansible]$ ansible-playbook storage.yml --syntax-check
playbook: storage.yml
*Run the playbook
[lisa@drsdev1 ansible]$ ansible-playbook storage.yml
*verify the configuration
verify the block device size to ascertain the difference between GiB and GB as mentioned above.
[lisa@drsdev1 ansible]$ ansible hqsdev1.tekneed.com -a 'lsblk'
hqsdev1.tekneed.com | CHANGED | rc=0 >>
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 15G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 14G 0 part
├─rhel-root 253:0 0 12.5G 0 lvm /
└─rhel-swap 253:1 0 1.5G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 5G 0 part
sr0 11:0 1 7.3G 0 rom
verify that the volume group has been created.
[lisa@drsdev1 ansible]$ ansible hqsdev1.tekneed.com -a 'vgs'
hqsdev1.tekneed.com | CHANGED | rc=0 >>
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- <14.00g 0
tekneed-vg 1 0 0 wz--n- <4.97g <4.97g
Using The Ansible lvol module With Examples
The Ansible lvol module is used to manage logical volume (LV); such as creating, removing, and resizing logical volumes, etc.
The manual for this module can be checked by using the command, “ansible-doc lvol”.
For the ‘size’ option/argument/parameter for Ansible 2.8, the size of the logical volume, according to lvcreate(8) –size, by default in megabytes or optionally with one of [bBsSkKmMgGtTpPeE] units; or according to lvcreate(8) –extents as a percentage of [VG|PVS|FREE]; Float values must begin with a digit. Resizing using percentage values was not supported prior to 2.1. [Default: (null)]
Let’s see how the lvol module can be used with examples.
In the example above, we created a volume group with the name, tekneed-vg, hence, in this example, we are going to create the logical volume with the name, tekneed-lv and the size, 2gb.
*create a task to add to the playbook above.
[lisa@drsdev1 ~]$ cd ansible/
[lisa@drsdev1 ansible]$ vi storage.yml
-------------
- name: create a LV on tekneed-vg
lvol:
vg: tekneed-vg
lv: tekneed-lv
size: 2GB
This task will use the lvol module to create a LV(tekned-lv) with the size of 2gb using the size option from the VG(tekneed-vg)
At the end of the playbook creation, the whole playbook will look like this:
---
- name: configure storage
hosts: hqsdev1.tekneed.com
tasks:
- name: partition sdb to 1gb
parted:
device: /dev/sdb
number: 1
state: present
part_end: 5GiB
- name: create a VG on sdb1
lvg:
vg: tekneed-vg
pvs: /dev/sdb1
pesize: 32
- name: create a LV on tekneed-vg
lvol:
vg: tekneed-vg
lv: tekneed-lv
size: 2g
*Do a playbook syntax check
[lisa@drsdev1 ansible]$ ansible-playbook storage.yml --syntax-check
playbook: storage.yml
*Run the playbook.
[lisa@drsdev1 ansible]$ ansible-playbook storage.yml
PLAY [configure storage] ***********************************************************
......................
*Verify your configuration
[lisa@drsdev1 ansible]$ ansible hqsdev1.tekneed.com -a 'lvs'
hqsdev1.tekneed.com | CHANGED | rc=0 >>
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- <12.50g
swap rhel -wi-ao---- 1.50g
tekneed-lv tekneed-vg -wi-a----- 2.00g
Using The Ansible filesystem Module With Examples
The Ansible filesystem module is used to create filesystems.
To create the ext4 file system on the device, sdb2 on the hqsdev1.tekneed.com managed host for example, the playbook can be written as below:
[lisa@drsdev1 ansible]$ vi storage2.yml
- name: configure storage
hosts: hqsdev1.tekneed.com
tasks:
- name: create filesystem
filesystem:
fstype: ext4
dev: /dev/sdb2
*Run the playbook
[lisa@drsdev1 ansible]$ ansible-playbook storage2.yml
PLAY [configure storage] **********************************************
.......
Using The Ansible mount Module With Examples
The Ansible mount module is used to mount filesystems. It controls active and configured mount points in the “/etc/fstab” file.
Let’s see how the Ansible mount module can be used with examples.
From the example on logical volume creation above, the task below will be used to mount the LV created above (tekneed-lv) on the mount point, /tekneed.
------------
- name: mount tekneed-lv on /tekneed
mount:
path: /tekneed
src: /dev/tekneed-vg/tekneed-lv
fstype: ext3
state: present
The whole playbook will look like this:
---
- name: configure storage
hosts: hqsdev1.tekneed.com
tasks:
- name: partition sdb to 1gb
parted:
device: /dev/sdb
number: 1
state: present
part_end: 5GiB
- name: create a VG on sdb1
lvg:
vg: tekneed-vg
pvs: /dev/sdb1
pesize: 32
- name: create a LV on tekneed-vg
lvol:
vg: tekneed-vg
lv: tekneed-lv
size: 2g
- name: mount tekneed-lv on /tekneed
mount:
path: /tekneed
src: /dev/tekneed-vg/tekneed-lv
fstype: ext3
state: present
*Run the playbook
[lisa@drsdev1 ansible]$ ansible-playbook storage.yml
PLAY [configure storage] ************************************
.............................
*Verify your configuration
[lisa@drsdev1 ansible]$ ansible hqsdev1.tekneed.com -a 'cat /etc/fstab'
hqsdev1.tekneed.com | CHANGED | rc=0 >>
......................
/dev/tekneed-vg/tekneed-lv /tekneed ext3 defaults 0 0
Configuring Swap Memory Using Ansible
Ansible version 2.8 does not currently have a module to configure or manage swap. The only way swap can be configured with Ansible is to use the command module.
The task with a command module will be in the format below:
- name: create swap
command: mkswap /dev/vg/lv
The exam practice questions have good examples and production-like examples for configuring swap and managing storage as a whole using the Ansible engine.
Having understood the important modules used in managing storage, let’s finalize this topic with the example below.
create a playbook with the name, web.yml using a variable file with the name, web_var.yml in /home/lisa/ansible that will do the following:
-executes on hqdev1.tekneed.com managed hosts
-creates a partition of 400mb using the sdd device. (The floating range should be between 360-420)
-creates a volume group with the name, web-vg using the partitioned device.
-creates two logical volumes using the web-vg which are;
* web-lv, and size, 100mb, which should be formatted with xfs filesystem and mounted on /web
*web2-lv, and size, 150mb, which should be formatted by xfs filesystem, and mounted on /web2
Part B
-partition the device, sdd with a second partition by 200mb
-extend the volume group by the second partition
-Extend the logical volumes, web-lv, and web2-lv by 50mb each.
Answer
1. create the variable file, web_var.yml
[lisa@drdev1 ~]$ cd /home/lisa/ansible
[lisa@drdev1 ansible]$ vim web_var.yml
partitions:
- numb: 1
start: 1MiB
end: 400MiB
volume_groups:
- name: web-vg
devices: /dev/sdd1
logical_volumes:
- name: web-lv
size: 100m
v_group: web-vg
mount_path: /web
- name: web2-lv
size: 150m
v_group: web-vg
mount_path: /web2
2. create the playbook
[lisa@drdev1 ansible]$ vim web.yml
---
- name: configure storage
hosts: hqdev1.tekneed.com
vars_files:
- web_var.yml
tasks:
- name: create partitions
parted:
device: /dev/sdd
state: present
number: "{{ item.numb }}"
part_start: "{{ item.start }}"
part_end: "{{ item.end }}"
loop: "{{ partitions }}"
- name: create volume groups
lvg:
vg: "{{ item.name }}"
pvs: "{{ item.devices }}"
loop: "{{ volume_groups }}"
- name: create logical volume
lvol:
vg: "{{ item.v_group }}"
lv: "{{ item.name }}"
size: "{{ item.size }}"
loop: "{{ logical_volumes }}"
when: item.name not in ansible_lvm["lvs"]
- name: create filesystem
filesystem:
dev: "/dev/{{ item.v_group }}/{{ item.name }}"
fstype: xfs
loop: "{{ logical_volumes }}"
- name: mount volumes
mount:
path: "{{ item.mount_path }}"
src: "/dev/{{ item.v_group }}/{{ item.name }}"
fstype: xfs
opts: noatime
state: mounted
loop: "{{ logical_volumes }}"
3. Do a playbook syntax check.
[lisa@drdev1 ansible]$ ansible-playbook web.yml --syntax-check
playbook: web.yml
4. Verify if the LVs exists on the managed host if you wish
[lisa@drdev1 ansible]$ ansible hqdev1.tekneed.com -m setup
-a 'filter=ansible_lvm'
hqdev1.tekneed.com | SUCCESS => {
"ansible_facts": {
"ansible_lvm": {
"lvs": {
"data-lv": {
"size_g": "1.01",
...........
5. Run the playbook
[lisa@drdev1 ansible]$ ansible-playbook web.yml
PLAY [configure storage]
...........
6. Verify your configuration.
*Verify the VG
[lisa@drdev1 ansible]$ ansible hqdev1.tekneed.com -a 'vgs'
hqdev1.tekneed.com | CHANGED | rc=0 >>
VG #PV #LV #SN Attr VSize VFree
data-vg 2 2 0 wz--n- 3.99g 2.87g
rhel 1 2 0 wz--n- <14.00g 0
web-vg 1 2 0 wz--n- 396.00m 144.00m
*Verify the LVs
[lisa@drdev1 ansible]$ ansible hqdev1.tekneed.com -a 'lvs'
hqdev1.tekneed.com | CHANGED | rc=0 >>
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
data-lv data-vg -wi-ao---- 1.01g
web-lv data-vg -wi-a----- 112.00m
root rhel -wi-ao---- <12.50g
swap rhel -wi-ao---- 1.50g
web-lv web-vg -wi-ao---- 100.00m
web2-lv web-vg -wi-ao---- 152.00m
*You can also verify as block devices
[lisa@drdev1 ansible]$ ansible hqdev1.tekneed.com -a 'lsblk'
hqdev1.tekneed.com | CHANGED | rc=0 >>
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 15G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 14G 0 part
├─rhel-root 253:0 0 12.5G 0 lvm /
└─rhel-swap 253:1 0 1.5G 0 lvm [SWAP]
sdb 8:16 0 5G 0 disk
└─sdb1 8:17 0 2G 0 part
├─data--vg-data--lv 253:2 0 1G 0 lvm /data
└─data--vg-web--lv 253:5 0 112M 0 lvm
sdc 8:32 0 2G 0 disk
sdd 8:48 0 1G 0 disk
└─sdd1 8:49 0 399M 0 part
├─web--vg-web--lv 253:3 0 100M 0 lvm /web
└─web--vg-web2--lv 253:4 0 152M 0 lvm /web2
sr0 11:0 1 7.3G 0 rom
[lisa@drdev1 ansible]$ ansible hqdev1.tekneed.com -a 'df -h'
hqdev1.tekneed.com | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
devtmpfs 880M 0 880M 0% /dev
tmpfs 897M 0 897M 0% /dev/shm
tmpfs 897M 1.4M 895M 1% /run
tmpfs 897M 0 897M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 13G 6.0G 6.6G 48% /
/dev/mapper/data--vg-data--lv 988M 2.6M 918M 1% /data
/dev/sda1 1014M 238M 777M 24% /boot
tmpfs 180M 1.2M 179M 1% /run/user/42
tmpfs 180M 4.0K 180M 1% /run/user/0
/dev/mapper/web--vg-web--lv 95M 6.0M 89M 7% /web
/dev/mapper/web--vg-web2--lv 147M 8.9M 138M 7% /web2
PART B
1. Add the second partition to the variable file, and also add it to the volume_groups variable.
More so, Change the LVs sizes by adding 5omb each.
[lisa@drdev1 ansible]$ vim web_var.yml
partitions:
- numb: 1
start: 1MiB
end: 400MiB
- numb: 2
start: 400MiB
end: 600MiB
volume_groups:
- name: web-vg
devices: /dev/sdd1,/dev/sdd2
logical_volumes:
- name: web-lv
size: 150m
v_group: web-vg
mount_path: /web
- name: web2-lv
size: 200m
v_group: web-vg
mount_path: /web2
2. Edit the playbook file, and add a task to resize the LV and the filesystems
---
- name: configure storage
hosts: hqdev1.tekneed.com
vars_files:
- web_var.yml
tasks:
- name: create partitions
parted:
device: /dev/sdd
state: present
number: "{{ item.numb }}"
part_start: "{{ item.start }}"
part_end: "{{ item.end }}"
loop: "{{ partitions }}"
- name: create volume groups
lvg:
vg: "{{ item.name }}"
pvs: "{{ item.devices }}"
loop: "{{ volume_groups }}"
- name: create logical volume
lvol:
vg: "{{ item.v_group }}"
lv: "{{ item.name }}"
size: "{{ item.size }}"
loop: "{{ logical_volumes }}"
when: item.name not in ansible_lvm["lvs"]
- name: create filesystem
filesystem:
dev: "/dev/{{ item.v_group }}/{{ item.name }}"
fstype: xfs
loop: "{{ logical_volumes }}"
- name: ensure the correct size of LV and resize the filesystems
lvol:
vg: "{{ item.v_group }}"
lv: "{{ item.name }}"
size: "{{ item.size }}"
resizefs: yes
force: yes
loop: "{{ logical_volumes }}"
- name: mount volumes
mount:
path: "{{ item.mount_path }}"
src: "/dev/{{ item.v_group }}/{{ item.name }}"
fstype: xfs
opts: noatime
state: mounted
loop: "{{ logical_volumes }}"
3. Do a playbook syntax check
[lisa@drdev1 ansible]$ ansible-playbook web.yml --syntax-check
playbook: web.yml
4. Now run the playbook again so that the LVs can be extended and resized
[lisa@drdev1 ansible]$ ansible-playbook web.yml
PLAY [configure storage] *********************************************************************************
...........
5. Verify the new VG size
[lisa@drdev1 ansible]$ ansible hqdev1.tekneed.com -a 'vgs'
hqdev1.tekneed.com | CHANGED | rc=0 >>
VG #PV #LV #SN Attr VSize VFree
data-vg 2 2 0 wz--n- 3.99g 2.87g
rhel 1 2 0 wz--n- <14.00g 0
web-vg 2 2 0 wz--n- 592.00m 240.00m
6. Verify the new sizes of the LVs and the filesystems.
[lisa@drdev1 ansible]$ ansible hqdev1.tekneed.com -a 'lsblk'
........
hqdev1.tekneed.com | CHANGED | rc=0 >>
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdd 8:48 0 1G 0 disk
├─sdd1 8:49 0 399M 0 part
│ ├─web--vg-web--lv 253:3 0 152M 0 lvm /web
│ └─web--vg-web2--lv 253:4 0 200M 0 lvm /web2
└─sdd2 8:50 0 200M 0 part
sr0 11:0 1 7.3G 0 rom
*Verify the mounted filesystems
[lisa@drdev1 ansible]$ ansible hqdev1.tekneed.com -a 'df -h'
.............
/dev/mapper/web--vg-web--lv 147M 6.5M 141M 5% /web
/dev/mapper/web--vg-web2--lv 195M 9.4M 186M 5% /web2
Watch Video On Ansible lvol Module & Other Ansible Storage Related Module
RHCE Exam Practice Question On Ansible lvol Module & Other Ansible Storage Related Module
Your feedback is welcomed. If you love others, you will share with others
Leave a Reply