Ansible判断+实例
when
语句是 Ansible 中用于控制任务是否执行的核心指令
用于比较变量、字符串或数字。
运算符 | 描述 |
---|---|
== |
等于 |
!= |
不等于 |
> |
大于 |
< |
小于 |
>= |
大于等于 |
<= |
小于等于 |
用于组合多个条件。
运算符 | 描述 | 示例 |
---|---|---|
and |
逻辑与 | when: ansible_os_family == "RedHat" and ansible_processor_cores >= 2 |
or |
逻辑或 | when: ansible_distribution == "CentOS" or ansible_distribution == "Rocky" |
not |
逻辑非 | when: not webserver_enabled |
Defined:判断变量是否已经定义,已定义则返回为真
Undefined:判断变量是否已经定义,未定义则返回为真
None:判断变量值是否为空,如果变量已经定义,但是变量值为空,则返回为真
---
- name: a
hosts: node1
vars:
aa: 11
cc:
tasks:
- name: debug1
debug:
msg: aa
when: aa is defined
- name: debug2
debug:
msg: vvv
when: bb is undefined
- name: debug3
debug:
msg: add
when: cc is none
判断执行结果
状态判断条件 | 含义 |
---|---|
is success 或 succeeded | 判断任务是否执行成功 |
failure 或 failed | 判断任务是否执行失败 |
changed 或 change | 判断任务是否改变了系统状态 |
skipped或skip | 判断任务是否被跳过 |
---
- name: a
hosts: node1
vars:
aa: 11
tasks:
- name: b1
shell:
cmd: ls /mnt
when: aa==11
register: dd
- name: b2
debug:
msg: this success
when: dd is success
- name: b3
debug:
msg: this failed
when: dd is failed
- name: b4
debug:
msg: this changed
when: dd is changed
- name: b5
debug:
msg: this skip
when: dd is skip
以下tests的判断均对ansible主机中的路径,与目标主机无关
测试器 | 描述 |
---|---|
file | 判断路径是否是一个普通文件 |
directory | 判断路径是否是一个目录 |
link | 判断路径是否是一个符号链接(软链接) |
mount | 判断路径是否是一个挂载点 |
exists | 判断路径是否存在(可以是文件、目录、链接等) |
---
- name: a
hosts: node1
vars:
a1: /test/file1
a2: /test/
a3: /test/softlinka
a4: /test/hardlinka
a5: /boot/
tasks:
- name: de1
debug:
msg: file
when: a1 is file
- name: de2
debug:
msg: directory
when: a2 is directory
- name: de3
debug:
msg: softlinka
when: a3 is link
- name: de4
debug:
msg: hardlink
when: a4 is link
- name: de5
debug:
msg: mount
when: a5 is mount
- name: de6
debug:
msg: exists
when: a1 is exists
lower:判断包含字母的字符串中的字母是否纯小写
upper:判断包含字母的字符串中的字母是否纯大写
---
- name: a
hosts: node1
vars:
a1: aaabbbbcccc
a2: AAABBBCCC
a3: a222bbbc
tasks:
- name: t1
debug:
msg: all lower
when: a1 is lower
- name: t2
debug:
msg: all upper
when: a2 is upper
- name: t3
debug:
msg: aa22
when: a3 is lower
string:判断对象是否是一个字符串
number:判断对象是否一个数字
---
- name: a
hosts: node1
vars:
a1: 1
a2: "2"
a3: b
tasks:
- name: a11
debug:
msg: number
when: a1 is number
- name: a22
debug:
msg: string
when: a2 is string
- name: a33
debug:
msg: string
when: a3 is string
block/rescue/always
区块 | 类比编程语言 | 描述 |
---|---|---|
block | try |
主要执行区块。在这里定义您希望正常执行的核心任务。 |
rescue | catch |
救援区块。只有当 block 中的任何一个任务失败时,这里的任务才会执行。 |
always | finally |
总是执行区块。无论 block 成功还是失败(即使触发了 rescue ),这里的任务总是会执行 |
创建一个名为/etc/ansible/lv.yml 的playbook,它将在所有受管节点上运行以执行下列任务:
在node1、node2上添加一块硬盘,然后新建卷组
Node1的卷组大小为2G 卷组名为research
Node2的卷组大小为1G 卷组名为research
创建符合以下要求的逻辑卷:
逻辑卷创建在research卷组中
逻辑卷名称为data
逻辑卷大小为1500MiB
使用ext4文件系统格式化逻辑卷
如果无法创建请求的逻辑卷大小,应显示错误消息
Could not create logical volume of that size,并且应改为使用大小 800MiB。
如果卷组research 不存在 ,应显示错误消息
Volume group does not exist。
不要以任何方式挂载逻辑卷
1.新建卷组
---
- name: a
hosts: node1
tasks:
- name:
yum:
name: lvm2
state: present
- name: parted1
parted:
device: /dev/vdb
number: 1
part_type: primary
part_start: 10MiB
part_end: 2010MiB
state: present
- name: p1
lvg:
pvs: /dev/vdb1
vg: research
- name: n2
hosts: node2
tasks:
- name:
yum:
name: lvm2
state: present
- name: p222
parted:
device: /dev/vdb
number: 1
part_type: primary
part_start: 10MiB
part_end: 1000MiB
state: present
- name: p33
lvg:
vg: research
pvs: /dev/vdb1
2.创建逻辑卷
---
- name: c
hosts: all
tasks:
- name: c1
block:
- name: lv1
lvol:
vg: research
lv: data
size: 1500
rescue:
- name: c2
debug:
msg: Could not create logical volume of that size
- name: c3
lvol:
vg: research
lv: data
size: 800
always:
- name: c4
filesystem:
dev: /dev/research/data
fstype: ext4
when: "'research' in ansible_lvm.vgs"
- name: c5
debug:
msg: Volume group does not exist
when: "'research' not in ansible_lvm.vgs"
file_when能够中断剧本
---
- name: v1
hosts: node1
tasks:
- name: debug1
debug:
msg: 114514
- name: s1
shell:
cmd: echo 1234
register: bb
failed_when: "'12' in bb.stdout"
- name: s2
debug:
msg: xyh
ignore_errors: yes 跳过错误、忽略错误
Changed_when:可以修改任务执行后的最终状态,或者可以让任务执行状态显示失败
从 http://ansible.example.com/materials/newhosts.j2 下载模板文件
完成该模板文件,用来生成新主机清单(主机的显示顺序没有要求),结构如下:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.10 node1.example.com node1
192.168.122.20 node2.example.com node2
192.168.122.30 node3.example.com node3
192.168.122.40 node4.example.com node4
192.168.122.50 node5.example.com node5
创建剧本/home/student/ansible/newhosts.yml,它将使用上述模板在 test01 主机组的主机上
生成文件/etc/newhosts。
1.首先下载模板文件
[student@master ansible]$ curl -O http://ansible.example.com/materials/newhosts.j2
2.编辑newhosts文件
[student@master ansible]$ vim newhosts.j2
{% for xyh in groups.all %}
{{ hostvars[xyh].ansible_default_ipv4.address }} {{ hostvars[xyh].ansible_fqdn }} {{ hostvars[xyh].ansible_hostname }}
{% endfor %}
3.编辑剧本
[student@master ansible]$ vim newhosts.yml
---
- name: get fact
hosts: all
- name: cp
hosts: node1
tasks:
- name: cp1
template:
src: /home/student/ansible/newhosts.j2
dest: /etc/newhosts
查看node1的/etc/newhosts文件
编写剧本修改远程文件内容
创建剧本 /home/student/ansible/newissue.yml,满足下列要求:
1)在所有清单主机上运行,替换/etc/issue 的内容
2)对于 test01 主机组中的主机,/etc/issue 文件内容为 test01
3)对于 test02 主机组中的主机,/etc/issue 文件内容为 test02
4)对于 web 主机组中的主机,/etc/issue 文件内容为 Webserver
1.编辑newissue.yml剧本
[student@master ansible]$ vim newissue.yml
---
- name: rpl
hosts: all
tasks:
- name: t1
copy:
content: |
{% if 'test01' in group_names %}
test01
{% elif 'test02' in group_names %}
test02
{% elif 'web' in group_names %}
Webserver
{% endif %}
dest: /etc/issue
2.查看test01下的/etc/issue 的内容