一、条件判断基础
# 使用 when
tasks:
- name: shut down Debian flavored systems
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian"
# 多个条件语句:
...
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "6"
# 使用 and or:
...
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "CentOS" and ansible_distribution_major_version == "7")
# 使用 jinja2 的过滤器:
...
when: result|failed
# 可以读取变量的取值:
...
epic: true
...
when: epic
# 可以和循环一起使用:
tasks:
- command: echo {{ item }}
with_items: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5二、jinja模板判断
1、简介
注:本文demo使用ansible2.7稳定版
Jinja2的测试语句被用来评估一个条件表达式,并且最终返回True或False,经常和when语句搭配使用。
测试语句和过滤器的相同点:
测试语句的条件表达式也在控制端执行,在目的主机端生效。
测试语句和过滤器的不同点:
前者多被用于「比较」,执行结果是True或False,而后者多被用于对数据的操作与转换,执行结果是我们期望的数据内容或数据格式。
语法不同,前者使用「is」,后者使用「|」
测试语句的语法很简单,写法如下:
variable is test_name2、字符串
关键字 match 和 search,参数可以使用正则表达式,用来查找字符串是否与测试语句相匹配
关键字
match用于判断一个字符串的完整匹配,需完全匹配上才为true关键字
search用于判断一个字符串的包含匹配,后跟的字符串只要存在即为true
vars:
url: "http://example.com/users/foo/resources/bar"
tasks:
- debug:
msg: "matched pattern 1"
when: url is match("http://example.com/users/.*/resources/.*")
- debug:
msg: "matched pattern 2"
when: url is search("/users/.*/resources/.*")
- debug:
msg: "matched pattern 3"
when: url is search("/users/")3、判断版本号
关键字version用于判断版本号
# 判断版本号语法:
v1 is version(v2,'>=')
v1 is version('1.25.6','>=')
# version接收的运算符如下:
<, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne
# version也可以接收 [strict] 参数,这个参数的默认值为false,如果设置为true,则ansible会进行更加严格的版本检查
{{ sample_version_var is version('1.0', operator='lt', strict=True) }}4、测试列表
# 关键字「superset」和「subset」,用于测试一个列表是否包含或被包含于另一个列表:
vars:
a: [1,2,3,4,5]
b: [2,3]
tasks:
- debug:
msg: "A includes B"
when: a is superset(b)
- debug:
msg: "B is included in A"
when: b is subset(a)
# 关键字「all」和「any」,用于检查列表里的元素的真假:
vars:
mylist:
- 1
- "{{ 3 == 3 }}"
- True
myotherlist:
- False
- True
tasks:
- debug:
msg: "all are true!"
when: mylist is all
- debug:
msg: "at least one is true"
when: myotherlist is any
# 用大学学的离散数学概括下:
all:一假则假
any:一真则真5、测试文件路径
测试文件路径的关键字从字面上就能看出来其含义,下面直接上示例:
# 判断为目录()
- debug:
msg: "path is a directory"
when: mypath is directory
- debug:
msg: "path is a file"
when: mypath is file
- debug:
msg: "path is a symlink"
when: mypath is link
- debug:
msg: "path already exists"
when: mypath is exists
- debug:
msg: "path is {{ (mypath is abs)|ternary('absolute','relative')}}"
- debug:
msg: "path is the same file as path2"
when: mypath is same_file(path2)
- debug:
msg: "path is a mount"
when: mypath is mount注:
这个特性在ansible>=2.5的版本中才有,在生产实践中,可以替换旧版本中「stat」+「register」+「when」实现的功能。
以上对 tmp 、root 、 home下的目录、文件不生效
6、测试任务执行结果
测试任务执行结果也比较通俗易懂,示例如下:
- name: test
hosts: test
gather_facts: no
vars:
mytmp: /etc/docker/daemon.json
myfile: /boot
tasks:
- name: test
command: whoami
register: test
when: myfile is file
- name: debug
debug:
msg: "skipped"
when: test is skipped
- debug:
msg: "it failed"
when: test is failed
- debug:
msg: "it changed"
when: test is changed
- debug:
msg: "it succeeded in Ansible >= 2.1"
when: test is succeeded
- debug:
msg: "it succeeded"
when: test is success
- debug:
msg: "it was skipped"
when: test is skipped从结果来看,跳过会被当成成功处理
PLAY [test] ********************************************************************************************************
TASK [test] ********************************************************************************************************
skipping: [192.168.2.20]
skipping: [192.168.2.21]
TASK [debug] ********************************************************************************************************
ok: [192.168.2.20] => {
"msg": "skipped"
}
ok: [192.168.2.21] => {
"msg": "skipped"
}
TASK [debug] ********************************************************************************************************
skipping: [192.168.2.20]
skipping: [192.168.2.21]
TASK [debug] ********************************************************************************************************
skipping: [192.168.2.20]
skipping: [192.168.2.21]
TASK [debug] ********************************************************************************************************
ok: [192.168.2.20] => {
"msg": "it succeeded in Ansible >= 2.1"
}
ok: [192.168.2.21] => {
"msg": "it succeeded in Ansible >= 2.1"
}
TASK [debug] ********************************************************************************************************
ok: [192.168.2.20] => {
"msg": "it succeeded"
}
ok: [192.168.2.21] => {
"msg": "it succeeded"
}
TASK [debug] ********************************************************************************************************
ok: [192.168.2.20] => {
"msg": "it was skipped"
}
ok: [192.168.2.21] => {
"msg": "it was skipped"
}
PLAY RECAP ********************************************************************************************************
192.168.2.20 : ok=4 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
192.168.2.21 : ok=4 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
评论区