使用 Ansible 剧本(Playbook)进行 LVM 分区管理是一种自动化的方式,可以帮助管理员在多台主机上批量管理逻辑卷。
部署环境
3台主机,添加硬盘
ansible-galaxy collection install community.general 联网执行,下载兼容parted模块的模块吧
显示:
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/download/community-general-7.2.1.tar.gz to /root/.ansible/tmp/ansible-local-33201jai89ehw/tmplhtqs594/community-general-7.2.1-kh48eyd5
Installing 'community.general:7.2.1' to '/root/.ansible/collections/ansible_collections/community/general'
community.general:7.2.1 was installed successfully
操作
1.执行前奏脚本(格式非常重要,格式不对无法执行,)
- name: create volume group #任务名hosts: group1 tasks:- name: Create partition for LVM community.general.parted: #引用模块device: /dev/nvme0n2 #硬盘设备路径number: 1 # 分区序号flags: [ lvm ] #分区标记state: present #操作方式present创建 absent删除 info查信息(默认part_start: 1MiB #分区起始位置part_end: 2GiB #分区结束位置- name: create research vg lvg:vg: research #卷组名称pvs: /dev/nvme0n2p1 #指定物理卷
- name: create volume grouphosts: group2tasks:- name: Create partition for LVMcommunity.general.parted:device: /dev/nvme0n2number: 1flags: [ lvm ]state: presentpart_start: 1MiBpart_end: 1GiB- name: create research vglvg:vg: researchpvs: /dev/nvme1n2p1
2.创建脚本lv.yml 按照以下的格式 data(lvm逻辑卷名称)(research卷组名称)
---
- name: create lvhosts: alltasks:- name: error ##当不存在vg时报错debug:msg: Volume group done not existwhen: ansible_facts['lvm']['vgs']['research'] is undefined- name: create lvblock: ##创建1000M的lv- name: create 1500m lvlvol:vg: researchlv: datasize: 1500mwhen: ansible_facts['lvm']['vgs']['research'] is definedrescue: ##创建1000M失败时,先报错,再创建800M的lv- name: debugdebug:msg: Could not create logical volume of that sizewhen: ansible_facts['lvm']['vgs']['research'] is defined- name: create 800m lvlvol:vg: researchlv: datasize: 800mwhen: ansible_facts['lvm']['vgs']['research'] is definedalways: ##对创建的lv进行格式化- name: filesystemfilesystem:fstype: ext4dev: /dev/research/datawhen: ansible_facts['lvm']['vgs']['research'] is defined
解析
---
- name: create lvhosts: all ##主机tasks:- name: error ##当不存在vg时报错debug:msg: Volume group done not exist ##显示的信息when: ansible_facts['lvm']['vgs']['research'] is undefined ##不存在时- name: create lvblock: ##创建1000M的lv- name: create 1500m lvlvol:vg: research ##卷组名lv: data ##逻辑卷名size: 1500m ##内存大小when: ansible_facts['lvm']['vgs']['research'] is defined ##存在时rescue: ##创建1000M失败时,先报错,再创建800M的lv- name: debugdebug:msg: Could not create logical volume of that size ##显示的信息when: ansible_facts['lvm']['vgs']['research'] is defined- name: create 800m lv ##创建800m的lvlvol:vg: research ##卷组名lv: data ##逻辑卷名size: 800m ##内存大小when: ansible_facts['lvm']['vgs']['research'] is defined ##存在时always: ##对创建的lv进行格式化- name: filesystemfilesystem:fstype: ext4 ##ext4格式dev: /dev/research/data ##dev文件when: ansible_facts['lvm']['vgs']['research'] is defined ##存在时