在有的时候play的结果依赖于变量、fact或者是前一个任务的执行结果,或者有的时候,我们会基于上一个task执行返回的结果而决定如何执行后续的task。这个时候就需要用到条件判断。
条件语句在Ansible中的使用场景:
在ansible中,使用条件判断的关键字就是when。
如在安装包的时候,需要指定主机的操作系统类型,或者是当操作系统的硬盘满了之后,需要清空文件等,可以使用when语句来做判断 。when关键字后面跟着的是python的表达式,在表达式中你能够使用任何的变量或者fact,当表达式的结果返回的是false,便会跳过本次的任务
下面是一个基本的用法示例:
---
- name: Install vim
hosts: all
tasks:
- name:Install VIM via yum
yum:
name: vim-enhanced
state: installed
when: ansible_os_family =="RedHat"
- name:Install VIM via apt
apt:
name: vim
state: installed
when: ansible_os_family =="Debian"
- name: Unexpected OS family
debug: msg="OS Family {{ ansible_os_family }} is not supported" fail=yes
when: not ansible_os_family =="RedHat" or ansible_os_family =="Debian"
也可以使用bool值作为when的判断条件:
- hosts: all
user: root
vars:
epic: true
tasks:
- shell: echo "This certainly is epic!"
when: epic
- shell: echo "This certainly is not epic!"
when: not epic
表达式 | 示例 |
---|---|
字条串相等 | ansible_machine == "x86_64" |
数字相等 | max_memory == 512 |
小于 | min_memory < 512 |
大于 | min_memory > 256 |
小于等于 | min_memory <=256 |
大于等于 | min_memory >= 256 |
不等于 | min_memory != 256 |
变量存在 | min_memory is defined |
变量不存在 | min_memory is not defined |
布尔值: 1, True, yes为真;0,False, no为假 | memory_available |
布尔值为假 | not meory_available |
第一个变量的值是否在第二个变量的列表当中 | ansible_distribution in supported_distros |
通过when判断变量是否存在:
tasks:
- shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
when: foo is defined
- fail: msg="Bailing out. this play requires 'bar'"
when: bar is not defined
# 只打印大于5的值
tasks:
- command: echo {{ item }}
loop: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
# 确保将mariadb-server安装到根分区且根分区的可用空间要大于300M
- name: install mariadb-server if enough space on root
yum:
name: mariadb-server
state;拉特st
loop: "{{ ansible_mounts }}"
when: item.mount == "/" and item.size_available > 300000000
- name: restart httpd if postfix is running
hosts: test
tasks:
- name: get postfix server 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
在Ansible中,多条件判断可使用and或者or来连接多条语句:
or示例:
when: ansible_distribution == "RedHat" or ansible_distribution == "Fedora"
and示例:
when: ansible_distribution_version == "7.5" and ansible_kernel == "3.10.0-327.el7.x86_64"
and的条件语句也可以直接写成如下形式:
when: - ansible_distribution_version == "7.5" - ansible_kernel == "3.10.0-327.el7.x86_64"
更复杂的条件语句示例:
when: =>
( ansible_distribution == "RedHat" and ansible_distribution_major_version == "7" )
or
( ansible_distribution == "Fedora" and ansible_distribution_major_version == "28")
有些时候,你也许想在一个Playbook中以不同的方式做事,比如说在debian和centos上安装apache,apache的包名不同,除了when语句,还可以使用下面的示例来解决:
---
- hosts: all
remote_user: root
vars_files:
- "vars/common.yml"
- [ "vars/{{ ansible_os_family }}.yml", "vars/os_defaults.yml" ]
tasks:
- name: make sure apache is running
service: name={{ apache }} state=running
很多不同的yml文件只是包含键和值,如下:
---
# for vars/CentOS.yml
apache: httpd
somethingelse: 42
如果操作系统是CentOS
, Ansible导入的第一个文件将是vars/CentOS.yml
,紧接着 是/var/os_defaults.yml
,在Debian系统中,最先查看的将是vars/Debian.yml
而不是vars/CentOS.yml
,如果没找到,则寻找默认文件vars/os_defaults.yml
。
有些时候,我们想基于不同的操作系统,选择不同的配置文件,及配置文件的存放路径,可以借助with_first_found
来解决:
# 将paths中的路径与files中的文件名拼接成一个完整的文件路径,在被控端找到哪个就优先使用哪个:
- name: template a file
template: src={{ item }} dest=/etc/myapp/foo.conf
with_first_found:
- files:
- httpd.conf
- apache2.conf
paths:
- /etc/httpd/conf/
- /usr/local/apache2/conf/
原文:https://www.cnblogs.com/breezey/p/10996632.html