Ansible(六)对目标主机进行磁盘分区,创建逻辑卷、格式化并挂载

1、首先在server5上添加一块虚拟的空间

[root@server5 ~]# fdisk -l     ##查看,然后真迹打开资源管理其添加虚拟空间
[root@foundation35 ~]# virt-manager     

Ansible(六)对目标主机进行磁盘分区,创建逻辑卷、格式化并挂载_第1张图片
2、在server4上编写.yml文件

[devopes@server4 ansible]# vim my_disk.yml
---
- hosts: test
  vars_files:
    - storage_vars.yaml
  tasks:
    - name: Create a new partition
      parted:
        device: /dev/vda
        number: "{{ item.number }}"
        state: present
        part_start: "{{ item.start }}"
        part_end: "{{ item.end }}"
      loop: "{{ partitions }}"
    
    - name: create volume group
      lvg:
        vg: "{{ item.vg }}"
        pvs: "{{ item.pvs }}"
      loop: "{{ volumegroup }}"

    - name: create logical volume
      lvol:
        vg: "{{ item.vg }}"
        lv: "{{ item.lv }}"
        size: "{{ item.size }}"
        resizefs: true
        force: yes
        state: present
      loop: "{{ logicalvolume }}"
      when: item.lv not in ansible_lvm["lvs"]

    - name: create a xfs 
      filesystem:
        fstype: "{{ item.fstype }}"
        dev: /dev/demo_vg2/lv2_test
      loop: "{{ filesystem }}"

    - name: mount lvs
      mount:
        path: "{{ item.path }}"
        src: /dev/demo_vg2/lv2_test
        fstype: xfs
        state: mounted
        opts: noatime
      loop: "{{ mountargs }}"

2、编辑变量文件

[devopes@server1 ansible]$ vim storage_vars.yml
---
partitions:
  - number: 1
    start: 1MiB
    end: 1GiB

  - number: 2
    start: 1GiB
    end: 2GiB

volumegroup:
  - vg: demo_vg2
    pvs: /dev/vda2
    
logicalvolume:
  - vg: demo_vg2
    lv: lv2_test
    size: 100%FREE
    
filesystem:
  - fstype: xfs
  
mountargs:
  - path: /var/www/html

3、执行文件

[devopes@server4 ansible]# ansible-playbook my_disk.yml     ###正确执行

4、在server5上查看是否创建并挂在成功

[root@server5 ~]# df -h | grep /var/www/html

在这里插入图片描述

你可能感兴趣的:(企业)