Ansible部署逻辑卷
# Ansible部署逻辑卷
# 查看文件树
[root@ansible ansible]# tree disk/
disk/
├── fdisk.yaml
└── storage.yaml
# 配置Ansible的hosts清单
[root@ansible ansible]# cat /etc/ansible/hosts
[server]
172.25.254.137
172.25.254.139
# 基础准备
- 需要部署的机子准备50G的磁盘
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 20G 0 disk
└─vda1 253:1 0 20G 0 part /
vdb 253:16 0 50G 0 disk
# 编写playbook
- 创建pv卷
- 检测到创建第二块磁盘的时候创建vg的卷组
- 创建lv逻辑卷
- 查找到创建的逻辑卷格式化xfs格式
- 将格式化好的逻辑卷挂在/mnt下面
[root@ansible disk]# cat fdisk.yaml
---
- hosts: server
vars_files:
- storage.yaml
tasks:
- name: Create a new partition
parted:
device: /dev/vdb
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_vg3/lv2_test
loop: "{{ filesystem }}"
- name: mount lvs
mount:
path: "{{ item.path }}"
src: /dev/demo_vg3/lv2_test
fstype: xfs
state: mounted
opts: noatime
loop: "{{ mountargs }}"
# 创建vars的变量文件
[root@ansible disk]# cat storage.yaml
---
partitions:
- number: 1
start: 1MiB
end: 1GiB
- number: 2
start: 1GiB
end: 2GiB
volumegroup:
- vg: demo_vg3
pvs: /dev/vdb2
logicalvolume:
- vg: demo_vg3
lv: lv2_test
size: 100%FREE
filesystem:
- fstype: xfs
mountargs:
- path: /mnt
# 执行脚本
[root@ansible disk]# ansible-playbook fdisk.yaml
PLAY [server] *************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [172.25.254.139]
ok: [172.25.254.137]
TASK [Create a new partition] *********************************************************************************************************************************************
ok: [172.25.254.137] => (item={u'start': u'1MiB', u'end': u'1GiB', u'number': 1})
ok: [172.25.254.139] => (item={u'start': u'1MiB', u'end': u'1GiB', u'number': 1})
ok: [172.25.254.137] => (item={u'start': u'1GiB', u'end': u'2GiB', u'number': 2})
ok: [172.25.254.139] => (item={u'start': u'1GiB', u'end': u'2GiB', u'number': 2})
TASK [create volume group] ************************************************************************************************************************************************
ok: [172.25.254.139] => (item={u'pvs': u'/dev/vdb2', u'vg': u'demo_vg3'})
ok: [172.25.254.137] => (item={u'pvs': u'/dev/vdb2', u'vg': u'demo_vg3'})
TASK [create logical volume] **********************************************************************************************************************************************
skipping: [172.25.254.137] => (item={u'lv': u'lv2_test', u'vg': u'demo_vg3', u'size': u'100%FREE'})
skipping: [172.25.254.139] => (item={u'lv': u'lv2_test', u'vg': u'demo_vg3', u'size': u'100%FREE'})
TASK [create a xfs] *******************************************************************************************************************************************************
ok: [172.25.254.137] => (item={u'fstype': u'xfs'})
ok: [172.25.254.139] => (item={u'fstype': u'xfs'})
TASK [mount lvs] **********************************************************************************************************************************************************
changed: [172.25.254.139] => (item={u'path': u'/mnt'})
changed: [172.25.254.137] => (item={u'path': u'/mnt'})
PLAY RECAP ****************************************************************************************************************************************************************
172.25.254.137 : ok=5 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.254.139 : ok=5 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
# 验证检查
[root@ansible disk]# ansible all -m shell -a "df -h"
172.25.254.137 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.1G 19G 6% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 17M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
tmpfs 379M 0 379M 0% /run/user/0
/dev/mapper/demo_vg3-lv2_test 1017M 33M 985M 4% /mnt
上次更新: 2023/11/28, 22:03:59