使用ansible剧本进行lvm分盘

使用 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 group
  hosts: group2
  tasks:
    - name: Create partition for LVM
      community.general.parted:
        device: /dev/nvme0n2
        number: 1
        flags: [ lvm ]
        state: present
        part_start: 1MiB
        part_end: 1GiB
    - name: create research vg
      lvg:
        vg: research
        pvs: /dev/nvme1n2p1

2.创建脚本lv.yml 按照以下的格式 data(lvm逻辑卷名称)(research卷组名称)

复制代码
---
- name: create lv
  hosts: all
  tasks:
    - name: error  ##当不存在vg时报错
      debug:
        msg: Volume group done not exist
      when: ansible_facts['lvm']['vgs']['research'] is undefined

    - name: create lv
      block:  ##创建1000M的lv
        - name: create 1500m lv
          lvol:
            vg: research
            lv: data
            size: 1500m
          when: ansible_facts['lvm']['vgs']['research'] is defined
      rescue:  ##创建1000M失败时,先报错,再创建800M的lv
        - name: debug
          debug:
            msg: Could not create logical volume of that size
          when: ansible_facts['lvm']['vgs']['research'] is defined
        - name: create 800m lv
          lvol:
            vg: research
            lv: data
            size: 800m
          when: ansible_facts['lvm']['vgs']['research'] is defined
      always:  ##对创建的lv进行格式化
        - name: filesystem
          filesystem:
            fstype: ext4
            dev: /dev/research/data
          when: ansible_facts['lvm']['vgs']['research'] is defined

解析

复制代码
---
- name: create lv
  hosts: all	##主机
  tasks:
    - name: error  ##当不存在vg时报错
      debug:
        msg: Volume group done not exist		##显示的信息
      when: ansible_facts['lvm']['vgs']['research'] is undefined		##不存在时

    - name: create lv
      block:  ##创建1000M的lv
        - name: create 1500m lv
          lvol:
            vg: research		##卷组名
            lv: data			##逻辑卷名
            size: 1500m		##内存大小
          when: ansible_facts['lvm']['vgs']['research'] is defined	##存在时
      rescue:  ##创建1000M失败时,先报错,再创建800M的lv
        - name: debug
          debug:
            msg: Could not create logical volume of that size		##显示的信息
          when: ansible_facts['lvm']['vgs']['research'] is defined
        - name: create 800m lv			##创建800m的lv
          lvol:
            vg: research      ##卷组名
            lv: data			##逻辑卷名
            size: 800m		##内存大小
          when: ansible_facts['lvm']['vgs']['research'] is defined	##存在时
      always:  ##对创建的lv进行格式化
        - name: filesystem
          filesystem:
            fstype: ext4		##ext4格式
            dev: /dev/research/data			##dev文件
          when: ansible_facts['lvm']['vgs']['research'] is defined		##存在时
相关推荐
꧁༺朝花夕逝༻꧂2 天前
Ansible小试牛刀
ansible
꧁༺朝花夕逝༻꧂2 天前
Ansible+Zabbix-agent2快速实现对多主机监控
运维·ansible
m0_628017592 天前
项目四.高可用集群_ansible
ansible
云攀登者-望正茂3 天前
通过 Ansible 在 Windows 2022 上安装 IIS Web 服务器
ansible
云攀登者-望正茂5 天前
使用 Ansible 在 Windows 服务器上安装 SSL 证书
ansible
tianyuanwo7 天前
Ansible自动化运维全解析:从设计哲学到实战演进
运维·自动化·ansible
哆啦A梦15887 天前
在golang中如何将已安装的依赖降级处理,比如:将 go-ansible/[email protected] 更换为 go-ansible/@v1.1.7
开发语言·golang·ansible
lifeng43218 天前
在 CentOS 上将 Ansible 项目推送到 GitHub 的完整指南
centos·github·ansible
运维成长记13 天前
ansible-playbook 进阶 接上一章内容
linux·服务器·ansible
半桶水专家13 天前
Ansible 配置Playbook文件格式、关键字和语法详解
ansible