2024年10月7日 星期一

【IT Notes】RHCE測驗補充題 Create and use partitions

Create /home/student/ansible/partition.yml, which will create partitions on managed nodes:

  •  After sdb creating a 1500M primary partition, partition number 1, and format ext4
  •  All groups to permanently mount the partition to /data
  •  If there is not enough disk space, give prompt information  
"Could not create partition of that size"

  •  create 800M partition
  •  If sdb does not exist, a prompt message will be given 
"This disk is not exist"

【題前說明】
這題的做法很類似於第九題,只不過後者是建立邏輯卷宗,而這題是做磁碟切割,難度來說一樣,要會使用不常用的module,首先對每台node的sdb磁區切割出1500MB空間,並將磁區編號命名為1、檔案格式為ext4,根目錄上建立/data資料夾並掛載到sdb1,若磁碟空間不足,要輸出「Could not create partition of that size」的訊息,並將磁區空間分割降為800MB。若完全不存在sdb磁區則是直接輸出「this disk is not exist」。

一.解題過程:

[student@workstation ansible]$ vim partition.yml
---
- name: create new partition
  hosts: all
  tasks:
    - name: create new partition
      block:
        - name: create 1500MiB partition
          parted:
            device: /dev/sdb
            number: 1
            state: present
            part_end: 1500MiB
        - name: format filesystem
          filesystem:
            fstype: ext4
            dev: /dev/sdb1
      rescue:
        - name: output error msg
          debug:
            msg: "Could not create partiontion of that size"
        - name: create 800MiB partition
          parted:
            device: /dev/sdb
            number: 1
            state: present
            part_end: 800MiB
      always:
        - name: create directory data
          file:
            path: /data
            state: directory
        - name: mount data on device
          mount:
            src: /dev/sdb1
            path: /data
            state: mounted
            fstype: ext4
      when: ansible_devices.sdb is defined
    - name: output error msg
      debug:
          msg: "This disk is not exist"
      when: ansible_devices.sdb is not defined

二.驗證結果

[student@workstation ansible]$ ansible-playbook -C partition.yml

紅色警告是因為dry run沒有真切割,所以這是正常訊息

[student@workstation ansible]$ ansible-playbook partition.yml

正式跑就都一切正常

[student@workstation ansible]$ ansible all -m shell -a 'df -h'

輸入指令來檢查每台node所分割的新磁區


三.恢復解題前的環境
[student@workstation ansible]$ vim 19-lab-parted-stop.yml
---
- name: remove parted
  hosts: all
  tasks:
    - name: remove directory
      block:
        - name: unmount /data
          mount:
            path: /data
            state: unmounted
        - name: remove directory
          file:
            path: /data
            state: absent
          
    - name: remove parted
      block:
        - name: remove parted
          parted:
            device: /dev/sdb
            number: 1
            state: absent

- name: remove partition.yml
  hosts: 127.0.0.1
  tasks:
    - name: remove partition.yml
      file:
        path: /home/student/ansible/partition.yml
        state: absent


[student@workstation ansible]$ ansible-playbook 19-lab-parted-stop.yml


最後這題補充題沒有考出來,不過有做練習就一樣放上來,個人感覺實用性不高,很少人會用ansible部署的方式來分割磁區,總之,這段時間所做的練習和紀錄可告一段落了,想想這過程真的很漫長,從開始上課到今天完成所有RHCE筆記,中間經歷過三年時間,當然只是我自己偷懶,其實應該要從RHCSA就開始做筆記了,不過光是RHCE就總共練習了快要20題,這樣寫下來還真是累人,後面的路程還很漫長,還是那句老話,再接再厲吧!

沒有留言:

張貼留言

【IT Notes】透過api移轉Gmail到Exchange

 在雲端裡面串接api不是一件很好學的技術,第一次有機會學習到將GWS的Gmail信件全部轉移到M365的Exchange,其實方法很多種,像以前用的pst檔匯出轉移的方式等,但透過api串接,可以批次和排程轉移,是非常方便且準確的作法。唯一讓人感到困難的是學習成本不小,通常需要...