概述
在playbook中,条件语句when,可以控制在不同条件情况下执行不同的task或者role。
用于条件语句的变量值,可以是自定义变量,也可以是ansible_facts。
常用的ansible_facts:Conditionals — Ansible Documentation
示例
tasks中使用条件语句
tasks:
- name: copy etcd config to etcd2
template: src=etcd.conf.yml-etcd2.j2 dest=/home//etcd-v3.4.14-linux-amd64/etcd.conf.yml
when: hostname=="etcd-2"
roles使用条件语句
- hosts: webservers
roles:
- role: debian_stock_config
when: ansible_facts['os_family'] == 'Debian'
include使用条件语句
# main.yml
- include_tasks: other_tasks.yml
when: x is not defined