• 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
  • 相关阅读:
    linux查看日志命令
    百度智能小程序源码系统:打造极致用户体验的关键 带完整搭建教程
    使用python读写xlsx格式中的数据【xlrd、pywin32】
    各种排序算法性能对比
    安全协议内涵
    数据库系统概论——数据库恢复技术
    2023 年高教社杯全国大学生数学建模竞赛获奖名单(初稿)公示
    面向大型语言模型的低功耗加速–高通云人工智能软件开发工具包
    SDUT—Python程序设计实验六(字典与集合)
    001 PE急救盘部署
  • 原文地址:https://blog.csdn.net/Sebastien23/article/details/127852523