Create a playbook called /home/studnet/ansible/lvm.yml that runs on all managed nodes that does the following:
Creates a logical volume with these requirements:
- The logical volume is created in the research volume group
- The logical volume name is data
- The logical volume size is 1500 MiB
Formats the logical volume with the ext4 filesystem
If the requested logical volume size cannot be created, the error message
Could not create logical volume of that size
should be displayed and the size 800 MiB should be used instead.
If the volume group research does not exist, the error message
Volume group does not exist
should be displayed.
Does NOT mount the logical volume in any way.
【題前說明】
這題是較困難的題目之一,如果是用指令處理的話很快,但要用ansible部署的話就變得比要複雜,主要是要用到的module較多一些。基本的邏輯是,主機上research這個volume group者,先建立一個1500MB的lvm,如果空間不夠,族輸出「Could not create logical volume of that size」的訊息,然後改成800MB,並且命名為data;主機上沒有research這個volume group的話,則輸出「Volume group does not exist」的訊息。
這題是較困難的題目之一,如果是用指令處理的話很快,但要用ansible部署的話就變得比要複雜,主要是要用到的module較多一些。基本的邏輯是,主機上research這個volume group者,先建立一個1500MB的lvm,如果空間不夠,族輸出「Could not create logical volume of that size」的訊息,然後改成800MB,並且命名為data;主機上沒有research這個volume group的話,則輸出「Volume group does not exist」的訊息。
一.解題過程:
[student@workstation ansible]$ vim lv.yml
---
- name: create lvm
hosts: all
tasks:
- name: create lvm
block:
- name: create lvm 1500m
lvol:
lv: data
vg: research
size: 1500m
rescue:
- name: output error msg
debug:
msg: "Could not create logical volume of that size"
- name: create lvm 800m
lvol:
lv: data
vg: research
size: 800m
always:
- name: format filesystem
filesystem:
fstype: ext4
dev: /dev/research/data
when: ansible_lvm.vgs.research is defined
- name: output error msg
debug:
msg: "Volume group does not exist"
when: ansible_lvm.vgs.research is not defined
二.驗證結果
[student@workstation ansible]$ ansible-playbook -C lv.yml \\試跑看看是否正常
[student@workstation ansible]$ ansible-playbook lv.yml \\沒問題就正式跑
三.恢復解題前的環境
恢復解題前環境,先把node1和node2上名稱為date的logical volume刪除,然後workstation上的lv.yml也移除。
[student@workstation ansible]$ vim 09-lab-lv-stop.yml
---
- name: remove lv
hosts: node1,node2
tasks:
- name: remove lv
block:
- name: remove lv
lvol:
vg: research
lv: data
state: absent
force: yes
- name: remove lv file
hosts: 127.0.0.1
tasks:
- name: remove lv file
file:
path: /home/student/ansible/lv.yml
state: absent
[student@workstation ansible]$ ansible-playbook 09-lab-lv-stop.yml
沒有留言:
張貼留言