• 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
  • 相关阅读:
    面试题复盘-2023/10/20
    《Java核心知识点》+《Java面试宝典》+《1000道互联网面试专题》+《350道Java面试》,总共1045页
    uniapp高德地图ios 使用uni.chooseLocation选取位置显示没有搜索到相关数据
    lua基础之Base
    Unity UGUI的InputField(输入框)组件的介绍及使用
    抽卡程序模拟
    学习和巩固mysql的经典练习题目
    JVM虚拟机中如何判断对象可以回收
    pytorch应用于MNIST手写字体识别
    N种实用功能,助力企业智破服务难题
  • 原文地址:https://blog.csdn.net/Sebastien23/article/details/127852523