Ansible创建逻辑卷:
环境准备:
清单文件:
[dev]
192.168.110.129
[prod]
192.168.110.132
[all:vars]
ansible_user=root
ansible_password=redhat
磁盘准备:
在一台主机上添加一块sata接口的磁盘,一块不添加。
192.168.110.129 | CHANGED | rc=0 >>
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
利用129主机上的sdb来创建逻辑卷,而132上没有sdb所以可以形成对比。
准备卷组:
129主机:
[root@client ~]# fdisk /dev/sdb
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): e
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +1800M
Created a new partition 1 of type 'Extended' and of size 1.8 GiB.
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Extended' to 'Linux LVM'.
Command (m for help): w
[root@client ~]# vgcreate -s 16M myvg /dev/sdb1
yml文件:
---
- name: create lv
hosts: all
tasks:
- name: check vg
shell: vgs myvg
ignore_errors: yes
register: res
- name: print vg err
debug:
msg: "vg is not exits"
when: res is failed
failed_when: res is failed
- name: create start
lvol:
vg: myvg
size: '1500'
lv: data
when: "'myvg' in ansible_lvm.vgs"
执行结果:
检查lv:
提示:
ignore_errors
shell执行后的结果若为错则会导致整个剧本无法继续,因此需要该指令用于忽视返回的结果。
register
将返回结果注册到一个变量中。
when
用于判断的语句类似于其他编程语言中的if。
failed_when
是failed when的结合体,当返回结果为失败时,终端任务的执行。
以上解释全为我个人理解,大家可以看看就行不必太较真,有想法可以留言或邮箱联系讨论。
注:文章有问题请联系更正:wgq3135@163.com