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

该博客展示了如何使用Ansible playbook自动化地进行磁盘管理。首先,它演示了如何格式化整个磁盘/dev/sdb为xfs文件系统,并挂载到/data目录,同时配置开机自动挂载。接着,对于其他服务器,它介绍了如何创建VG、LV,同样格式化为xfs,并完成类似的操作。整个过程详细阐述了Ansible playbook在磁盘管理和文件系统挂载中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

将磁盘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)

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

- name: Broker server - make datadir
  file:
    path: /data
    state: directory
  when: ('broker_az1' in group_names) or ('broker_az2' in group_names)

获取磁盘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)

/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)

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

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

案例二:创建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)

利用创建好的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)

将创建好的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)

创建要挂载的文件系统目录/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)

获取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)

/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)

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

- 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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GottdesKrieges

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值