Ansible的条件判断介绍和使用方式详解!(2)
导读:Ansible的条件判断介绍和使用方式详解,# 逻辑与when: ansible_disibution == "CentOS" and ansible_disibution_major_vsion == "7"# 逻辑或when: ansible_disibution == "RedHat" or ansible_disibution == "Fedora"when: - ansible_disibution_vsi
Ansible的条件判断介绍和使用方式详解
# 逻辑与
when: ansible_disibution == "CentOS" and ansible_disibution_major_vsion == "7"
# 逻辑或
when: ansible_disibution == "RedHat" or ansible_disibution == "Fedora"
when:
- ansible_disibution_vsion == "7.9"
- ansible_kernel == "3.10.0-327.el7.x86_64"
# 组合使用
when: =>
( ansible_disibution == "RedHat" and ansible_disibution_major_vsion == "7" )
or
( ansible_disibution == "Fedora" and ansible_disibution_major_vsion == "28")
示例:
- name: uninstall and stop forewalld
hosts: dbsrvs
tasks:
- name: uninstall firewalld
yum: pkg=firwalld state=absent
when: ansible_disibution == "CentOS" and ansible_disibution_major_vsion == "7"
tags: uninstall_firewalld
- name: stop and disabled iptables
shell: systemctl stop firewalld.service && systemctl disable firewalld && systemctl stop iptables && systemctl disable iptables
when: ansible_disibution == "CentOS" and ansible_disibution_major_vsion == "7"
tags: stop_firewalld
###
- name: restart httpd if postfix is running
hosts: dbsrvs
tasks:
- name: get postfix serv status
command: /usr/bin/systemctl is-active postfix
ignore_errors: yes
register: result
- name: restart apache httpd based on postfix status
service:
name: httpd
state: restarted
when: result.rc == 0
tests 配合条件判断
通过条件语句判断tpath的路径是否存在
- hosts: dbsrvs
vars:
tpath: /bunianSky
tasks:
- debug:
msg: "file exist"
when: tpath is exists
参数解释:
- is exists: 用于路径存在时返回真
- is not exists: 用于路径不存在时返回真
- 也可以在整个条件表达式的前面使用not来取反
- hosts: dbsrvs
vars:
tpath: /bunianSky
tasks:
- debug:
msg: "file not exist"
when: not tpath is exists
除了 exists 方式以外,还有其他的判断方式,如下:
判断变量
- defined:判断变量是否已定义,已定义则返回真
- undefined:判断变量是否未定义,未定义则返回真
- none:判断变量的值是否为空,如果变量已定义且值为空,则返回真
- hosts: dbsrvs
gather_facts: no
vars:
tvar: "test"
tvar1:
tasks:
- debug:
msg: "tvar is defined"
when: tvar is defined
- debug:
msg: "tvar2 is undefined"
when: tvar2 is undefined
- debug:
msg: "tvar1 is none"
when: tvar1 is none
判断执行结果
- sucess或succeeded:通过任务执行结果返回的信息判断任务的执行状态,任务执行成功则返回true
- failure或failed:任务执行失败则返回true
- change或changed:任务执行状态为changed则返回true
- skip或skipped:任务被跳过则返回true
相关阅读
-
telnet命令不可用 telnet不是内部或外部命令怎么办
今天小编介绍telnet不是内部或外部命令怎么办的话题,关于telnet命令不可用 telnet不是内部或外部命令怎么办,接下来IT袋小编就来介绍。 在使用win10系统的过程中想必大家都遇到过一个共同的
-
win10玩游戏比win7流畅吗 win7玩游戏流畅还是win10
今天小编介绍win10玩游戏比win7流畅吗方面的介绍,关于win10玩游戏比win7流畅吗 win7玩游戏流畅还是win10,下面IT袋为您详细介绍 现在不少的IT袋网友都会在电脑上玩游戏。同时,也有人会提出疑
-
win10怎么修改电脑的开机密码 电脑开机密码设置步骤
您可能不了解win10怎么修改电脑的开机密码和电脑开机密码设置步骤的介绍,很不错的方法小知识,建议收藏哦! 在我们日常生活中,最常见的就是开机密码了,而电脑作为我们使用频率最高
-
win10系统电脑蓝屏怎么解决 win10电脑蓝屏了怎么处理方法
文章摘要:win10系统电脑蓝屏怎么解决IT技巧方面的经验,关于win10系统电脑蓝屏怎么解决 win10电脑蓝屏了怎么处理方法,下面小编为您详细解答 win10系统是一款非常优秀的智能电脑系统,在全


