• ansible playbook实现磁盘格式化及文件系统挂载


    案例一:格式化整个磁盘并挂载文件系统

    将磁盘sdb格式化为xfs文件系统:

    - name: Broker server - make filesystem
      filesystem:
        fstype: xfs
        dev: /dev/sdb
        opts: -n ftype=1 -L "data"
      when: ('broker_az1' in group_names) or ('broker_az2' in group_names)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建要挂载的文件系统目录/data

    - name: Broker server - make datadir
      file:
        path: /data
        state: directory
      when: ('broker_az1' in group_names) or ('broker_az2' in group_names)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    获取磁盘sdb的UUID并保存到变量中:

    - name: Broker server - get sdb uuid
      shell: 'blkid -s UUID /dev/sdb | cut -d " " -f2'
      register: uuid_sdb
      when: ('broker_az1' in group_names) or ('broker_az2' in group_names)
      
    - name: check uuid_sdb variable
      debug:
        msg: "uuid: {{uuid_sdb.stdout}}"
      when: ('broker_az1' in group_names) or ('broker_az2' in group_names)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    /etc/fstab中配置文件系统开机自动挂载:

    - name: Broker server - auto-mount config in /etc/fstab
      lineinfile:
        line: "{{uuid_sdb.stdout}} /data xfs defaults 0 0"
        state: present
        path: /etc/fstab
      when: ('broker_az1' in group_names) or ('broker_az2' in group_names)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    挂载文件系统并检查挂载情况:

    - name: Broker server - mount datadir & display info
      shell: "mount -a && df -h"
      when: ('broker_az1' in group_names) or ('broker_az2' in group_names)
    
    • 1
    • 2
    • 3

    案例二:创建LV并挂载文件系统

    利用磁盘vdb创建VG(此过程中会自动创建PV):

    - name: Other servers - create vg_tdmq using /dev/vdb
      lvg:
        vg: vg_tdmq
        state: present
        pvs: /dev/vdb
      when: ('broker_az1' not in group_names) and ('broker_az2' not in group_names) and ('bookie_az1' not in group_names) and ('bookie_az2' not in group_names)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    利用创建好的VG来创建LV:

    - name: Other servers - create LV data using vg_tdmq
      lvol:
        vg: vg_tdmq
        lv: lv_data
        size: 100%FREE
        state: present
      when: ('broker_az1' not in group_names) and ('broker_az2' not in group_names) and ('bookie_az1' not in group_names) and ('bookie_az2' not in group_names)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    将创建好的LV格式化为xfs文件系统:

    - name: Other servers - make filesystem 
      filesystem:
        fstype: xfs
        dev: /dev/vg_tdmq/lv_data
        opts: -n ftype=1 -L "data"
      when: ('broker_az1' not in group_names) and ('broker_az2' not in group_names) and ('bookie_az1' not in group_names) and ('bookie_az2' not in group_names)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建要挂载的文件系统目录/data

    - name: Other servers - make datadir
      file:
        path: /data
        state: directory
      when: ('broker_az1' not in group_names) and ('broker_az2' not in group_names) and ('bookie_az1' not in group_names) and ('bookie_az2' not in group_names)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    获取lv_data的UUID并保存到变量中:

    - name: Other servers - get vdb uuid
      shell: 'blkid -s UUID /dev/mapper/vg_tdmq-lv_data | cut -d " " -f2'
      register: uuid_vdb_lvdata
      when: ('broker_az1' not in group_names) and ('broker_az2' not in group_names) and ('bookie_az1' not in group_names) and ('bookie_az2' not in group_names)
    
    • 1
    • 2
    • 3
    • 4

    /etc/fstab中配置文件系统开机自动挂载:

    - name: Other servers - auto-mount config in /etc/fstab
      lineinfile:
        line: "{{uuid_vdb_lvdata.stdout}} /data xfs defaults 0 0"
        state: present
        path: /etc/fstab
      when: ('broker_az1' not in group_names) and ('broker_az2' not in group_names) and ('bookie_az1' not in group_names) and ('bookie_az2' not in group_names)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    挂载文件系统并检查挂载情况:

    - name: Other servers - mount datadir & display info
      shell: "mount -a && df -h"
      when: ('broker_az1' not in group_names) and ('broker_az2' not in group_names) and ('bookie_az1' not in group_names) and ('bookie_az2' not in group_names)
    
    • 1
    • 2
    • 3
  • 相关阅读:
    均匀B样条曲线的表达式
    GenICam标准(二)
    2023-09-28力扣每日一题-差分
    【C语言】文件操作
    性能测试 —— Jmeter 命令行详细
    计算机毕业设计(附源码)python玉米生产力管理与分析平台
    GO语言框架中如何快速集成日志模块
    汇编语言之母逝世,71岁时还和儿子合写神经网络论文
    CCF CSP认证 历年题目自练Day37
    python 应用之 request 请求调用
  • 原文地址:https://blog.csdn.net/Sebastien23/article/details/127852523