2024年7月15日 星期一

【IT Notes】RHCE測驗第九題 CREATE AND USE A LOGICAL VOLUME

 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」的訊息。


一.解題過程:
[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 \\試跑看看是否正常

除了node1和node2都有名為research的volume group,其它三台沒有噴紅色的訊息是正常的


[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



沒有留言:

張貼留言

【少年回憶】尋夢谷

 「尋夢谷」這個地名,在我人生中是一個很深刻的存在。那大約是國中二年級的事情了吧,距離現在快要三十年了。後來重新找到這個地方的時候,這裡只剩下大門和一處房舍,尤其是後者,讓我一看到馬上就能辨識出來;至於剩下的其他地方,如今只剩下一片荒煙蔓草、汽車駕訓班和工寮。 為什麼此處留有我那...