RHCE8练习题18: parted创建和使用分区

创建一个名为 /home/student/ansible/partion.yml 的playbook 它将在 所有 受管节点上创建分区:

  1. 在 vdb 分区上创建一个 1500M 的主分区,分区号 1 ,格式化为 ext4 , dev组中主机 将分区 永久 挂载到 /data
  2. 如果磁盘空间不足,给出提示信息,Could not create partiontion of that size , 并创建 800M 分区
  3. 如果vdb不存在,给出提示信息, This disk is not exist

提示: 模块 block rescue ,parted ,file ,filesystem , mount , debug


Answer:

vim /home/student/ansible/partion.yml
---
- name: create and use parted
  hosts: all 
  tasks:
    - name: create parted
      block:
        - name: create  1500M  partition
          parted: 
            device: /dev/vdb
            number: 1
            state: present
            part_end: 1500MiB
        - name: format fs
          filesystem:
            fstype: ext4
            dev: /dev/vdb1
        - name:
          block:
            - name: create directory
              file: 
                path: /data
                state: directory
            - name: mount
              mount:
                src: /dev/vdb1
                path: /data
                state: mounted
                fstype: ext4
          when: inventory_hostname in groups.dev
      rescue:
        - name: errors
          debug:
            msg: "Could not create partition of that size"
        - name: create 800M partition
            parted:
              device: /dev/vdb
              number: 1
              state: present
              part_end: 800MiB
      when: ansible_devices.vdb is defined

    - name: "error"
      debug: 
        msg: "this disk is not exist"
      when: ansible_devices.vdb is not defined

验证:

ansible all -a 'lsblk'
ansible dev -a 'cat /etc/fstab'

file file