實例–正常任務

  1. 腳本 lvfs1.yml 使用 block - rescue - always 語法
    1. when: 限定只有 fieldwork vg 存在時才執行。
    2. 在 fieldwork 新增一個名為 test,大小 200M 的 lv
    3. rescue: 如果無法新增就改新增 100M 的 lv,且印出 "Could not create logical volume of that size"。
    4. always: 新增後將其格式化為 ext4。
    [deyu1@kvm19 ansible]$ vim lvfs1.yml 
    [deyu1@kvm19 ansible]$ cat lvfs1.yml 
    ---
    - hosts: dev 
      tasks:
      - block:
        - name: Create a logical volume of 200m
          lvol:
            vg: fieldwork
            lv: test
            size: '200'
      
        rescue:
        - debug:
            msg: Could not create logical volume of that size
        - name: Create a logical volume of 100m
          lvol:
            vg: fieldwork
            lv: test
            size: '100'
    
        always:
        - name: Create a ext4
          filesystem:
            fstype: ext4
            dev: /dev/fieldwork/test
     
        when: ansible_lvm.vgs.fieldwork is defined
    
  2. ansible-playbook 執行 lvfs1.yml 腳本,正常新增 200M lv test,且成功格式化。
    [deyu1@kvm19 ansible]$ ansible-playbook lvfs1.yml
    
    PLAY [dev] *********************************************************************
    
    TASK [Gathering Facts] *********************************************************
    ok: [kvm11]
    
    TASK [Create a logical volume of 200m] *****************************************
    changed: [kvm11]
    
    TASK [Create a ext4] ***********************************************************
    changed: [kvm11]
    
    PLAY RECAP *********************************************************************
    kvm11 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    
  3. 登入 kvm11 查看 /dev/fieldwork/test 大小為 200M,且檔案格式為 ext4。
    [root@kvm11 ~]# lvdisplay /dev/fieldwork/test
      --- Logical volume ---
      LV Path                /dev/fieldwork/test
      LV Name                test
      VG Name                fieldwork
      LV UUID                LCXmQs-ZCJb-S5XF-rKWU-ZqB5-1EHD-m5TViN
      LV Write Access        read/write
      LV Creation host, time kvm11.deyu.wang, 2020-04-01 19:21:13 +0800
      LV Status              available
      # open                 0
      LV Size                200.00 MiB
      Current LE             50
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     8192
      Block device           253:2
    
    [root@kvm11 ~]# lsblk -f /dev/fieldwork/test
    NAME           FSTYPE LABEL UUID                                 MOUNTPOINT
    fieldwork-test ext4         f2bbe681-5832-4d4e-8765-9cd0ea1f2bee