Ansible创建逻辑卷:

环境准备:

清单文件:

[dev]192.168.110.129[prod]192.168.110.132[all:vars]ansible_user=rootansible_password=redhat

磁盘准备:

在一台主机上添加一块sata接口的磁盘,一块不添加。

192.168.110.129 | CHANGED | rc=0 >>Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes

利用129主机上的sdb来创建逻辑卷,而132上没有sdb所以可以形成对比。

准备卷组:

129主机:

[root@client ~]# fdisk /dev/sdbCommand (m for help): nPartition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions)Select (default p): ePartition 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): +1800MCreated a new partition 1 of type 'Extended' and of size 1.8 GiB.Command (m for help): tSelected partition 1Hex code (type L to list all codes): 8eChanged type of partition 'Extended' to 'Linux LVM'.Command (m for help): w[root@client ~]# vgcreate -s 16M myvg /dev/sdb1

yml文件:

---- name: create lvhosts: alltasks:- name: check vgshell: vgs myvgignore_errors: yesregister: res- name: print vg errdebug:msg: "vg is not exits"when: res is failedfailed_when: res is failed- name: create startlvol:vg: myvgsize: '1500'lv: datawhen: "'myvg' in ansible_lvm.vgs"

执行结果:


检查lv:

提示:

ignore_errors

shell执行后的结果若为错则会导致整个剧本无法继续,因此需要该指令用于忽视返回的结果。

register

将返回结果注册到一个变量中。

when

用于判断的语句类似于其他编程语言中的if

failed_when

是failed when的结合体,当返回结果为失败时,终端任务的执行。

以上解释全为我个人理解,大家可以看看就行不必太较真,有想法可以留言或邮箱联系讨论。

注:文章有问题请联系更正:wgq3135@163.com