-
给 node1 添加一块 5G 的硬盘;给 node2 添加一块 2G 的硬盘;node3 不添加硬盘。
-
写一个 partition.yml 的 playbook,满足如下要求:
-
给所有的受控主机创建分区,创建 2500M 的分区,然后对分区进行格式化,格式化为 xfs 的 文件系统。
-
如果不能满足创建 2500M 的分区,则显示错误消息为:disk size not enough,接着改为创 建 1000M。
-
如果没有 /dev/sdb 硬盘,则显示错误消息为:the device is not sdb
-
添加硬盘
# node1
[root@node1 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cs-root 253:0 0 17G 0 lvm /
└─cs-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 5G 0 disk
sr0 11:0 1 9G 0 rom
# node2
[root@node2 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cs-root 253:0 0 17G 0 lvm /
└─cs-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 2G 0 disk
sr0 11:0 1 9G 0 rom
# node3
[root@node3 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cs-root 253:0 0 17G 0 lvm /
└─cs-swap 253:1 0 2G 0 lvm [SWAP]
sr0 11:0 1 9G 0 rom
编写playbook
[root@ansible ~]#su - student
[student@ansible ~]$cd ansible/
[student@ansible ~/ansible]$vim partition.yml
---
- name: parted for node1
hosts: all
tasks:
- name: create partition 2500M
block:
- name: parted
parted:
device: /dev/sdb
number: 1
part_type: primary
part_start: 10MiB
part_end: 2510MiB
state: present
rescue:
- name: output fail message
debug:
msg: disk size not enough.
- name: create partition 1000M
parted:
device: /dev/sdb
number: 1
part_type: primary
part_start: 10MiB
part_end: 1010MiB
state: present
always:
- name: format
filesystem:
fstype: xfs
dev: /dev/sdb
when: "ansible_devices.sdb is defined"
- name: search not exists
debug:
msg: the device is not sdb
when: "ansible_devices.sdb is not defined"
[student@ansible ~/ansible]$ansible-playbook partition.yml
PLAY [parted for node1]
********************************************************************************
****************
TASK [Gathering Facts]
********************************************************************************
*****************
ok: [node1]
ok: [node2]
ok: [node3]
TASK [parted]
********************************************************************************
**************************
skipping: [node3]
fatal: [node2]: FAILED! => {"changed": false, "err": "Error: The location
2510MiB is outside of the device /dev/sdb.\n", "msg": "Error while running
parted script: /sbin/parted -s -m -a optimal /dev/sdb -- unit KiB mklabel msdos
mkpart primary 10MiB 2510MiB", "out": "", "rc": 1}
changed: [node1]
TASK [output fail message]
********************************************************************************
*************
ok: [node2] => {
"msg": "disk size not enough."
}
TASK [create partition 1000M]
********************************************************************************
**********
changed: [node2]
TASK [format]
********************************************************************************
**************************
skipping: [node3]
changed: [node2]
changed: [node1]
TASK [search not exists]
********************************************************************************
***************
skipping: [node1]
skipping: [node2]
ok: [node3] => {
"msg": "the device is not sdb"
}
PLAY RECAP
********************************************************************************
*****************************
node1 : ok=3 changed=2 unreachable=0 failed=0
skipped=1 rescued=0 ignored=0
node2 : ok=4 changed=2 unreachable=0 failed=0
skipped=1 rescued=1 ignored=0
node3 : ok=2 changed=0 unreachable=0 failed=0
skipped=2 rescued=0 ignored=0
标签:disk,练习,ansible,part,sdb,node1,node2
From: https://www.cnblogs.com/Archer-x/p/16846023.html