使用 Ansible 批量分区、创建逻辑卷、格式化、挂载

首先展示一下角色的目录结构

[root@ansible ~/ansible/vlan30_system_init/roles]# tree parted_mount/
parted_mount/
├── tasks
│   ├── create_lvs.yml
│   ├── device_status.yml
│   ├── main.yml
│   ├── mount_lvs.yml
│   ├── parted.yml
│   └── storage_pool.yml
└── vars
    └── main.yml

·

task 任务的 main.yml

cat parted_mount/tasks/main.yml 
---
# tasks file for parted_mount
- include: device_status.yml

- include: parted.yml
  when: device_status.stdout == "0" and device_part_status.stdout != "0"

- include: create_lvs.yml
  when: device_status.stdout == "0" and device_part_status.stdout != "0"

- include: mount_lvs.yml
  when: device_status.stdout == "0" and device_part_status.stdout != "0"

- include: storage_pool.yml
  when: device_status.stdout == "0" and device_part_status.stdout != "0"

首先通过 device_status.yml 获取磁盘是否正常加载的变量 device_status.stdout,和它是否已经含有分区的变量 device_part_status.stdout。当 device_status.stdout == "0" 时,表示磁盘加载正常;这时,再去判断它是否已经存在分区。当 device_part_status.stdout != "0" 时,表示磁盘无分区,即它是一个新的磁盘。

·

获取磁盘和分区的状态任务文件

cat parted_mount/tasks/device_status.yml 
---
#
- name: obtain "{{ DEVICE }}" status
  shell: AAA=$(fdisk -l 2> /dev/null |grep -o "{{ DEVICE }}");echo $?
  register: device_status

#- name: print device_status
#  debug:
#    msg: "{{ device_status }}"

- name: obtain "{{ DEVICE_PART }}"  status
  shell: BBB=$(lsblk -p | grep -o "{{ DEVICE_PART }}");echo $?
  register: device_part_status

#- name: print device_part_status
#  debug:
#    msg: "{{ device_part_status }}"

·

分区任务文件

cat parted_mount/tasks/parted.yml 
---
#
- name: Create a new primary partition for "{{ DEVICE }}"
  parted:
    device: "{{ DEVICE }}"
    label: gpt
    number: 1
    name: "{{ PART_NAME }}"
    part_start: "{{ PART_START }}"
    part_end: "{{ PART_END }}"
    state: present

·

创建逻辑卷任务文件

cat parted_mount/tasks/create_lvs.yml 
---
#
- name: Create a volume group on "{{ DEVICE_PART }}"
  lvg:
    vg: "{{ VGNAME }}"
    pvs: "{{ DEVICE_PART }}"

- name: Create a logical volume
  lvol:
    vg: "{{ VGNAME }}"
    lv: "{{ LVNAME }}"
    size: "{{ LVSIZE }}"

·

挂载任务文件

cat parted_mount/tasks/mount_lvs.yml 
---
#
- name: Create a xfs filesystem on logical volume "{{ LVNAME }}"
  filesystem:
    fstype: "{{ FSTYPE }}"
    dev: /dev/{{ VGNAME }}/{{ LVNAME }}

- name: Create "{{ MOUNT_PATH }}"
  file: 
    path: "{{ MOUNT_PATH }}"
    state: directory

- name: Mount up logical volume "{{ LVNAME }}"
  mount:
    path: "{{ MOUNT_PATH }}"
    src: /dev/{{ VGNAME }}/{{ LVNAME }}
    fstype: "{{ FSTYPE }}"
    state: present

- name: enable mount
  shell: mount -a

·

创建目录任务文件

cat parted_mount/tasks/storage_pool.yml 
---
#
- name: create Storage pool
  file: 
    path: "{{ item }}"
    state: absent
  loop: "{{ storage_pool_list }}"

·

变量的 main.yml

cat parted_mount/vars/main.yml 
---
# vars file for parted_mount
DEVICE: "/dev/sdb"

DEVICE_PART: "/dev/sdb1"

PART_NAME: "virt_img"

#Partition start offset
PART_START: "0%"

#Partition end offset
PART_END: "50%"

VGNAME: "Virt_Img_VG"

LVNAME: "Virt_Img_LV"

#Logical volume size
LVSIZE: "100%FREE"

FSTYPE: "xfs"

MOUNT_PATH: "/Virt_img"

storage_pool_list: 
 - "/Virt_img/iso"
 - "/Virt_img/Java_img"
 - "/Virt_img/Redis_img"
 - "/Virt_img/Mysql_img"

·

服务器配置初始化的剧本,有需要可参考文章:使用 Ansible 批量初始化服务器(最终版)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
>