根据不同的系统,安装不同的Apache版本
[student@workstation ansible]$ cat one.yml
- hosts: all
remote_user: root
tasks:
- name: 0.clean yum
shell: rm -rf /etc/yum.repos.d/*
when: ansible_os_family == "RedHat"
- name: 1.install ali source
get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo mode=0644 force=yes
when: ansible_os_family == "RedHat"
- name: 2.change yum
shell: sed -ri 's#\$releasever#7#g' /etc/yum.repos.d/CentOS-Base.repo && yum makecache
when: ansible_os_family == "RedHat"
- name: 3-1.Redhat family install web server
yum: name=httpd state=latest
when: ansible_os_family == "RedHat"
- name: 3-2.Debian faily install web server
apt: name=apache2 state=latest
when: ansible_os_family == "Debian"
判断变量是否在某个列表里
[student@workstation ansible]$ cat when_in.yml
- hosts: all
tasks:
- yum:
name: httpd
state: latest
#如果当前主机在webservers组中就执行安装httpd的任务
when: ansible_hostname in groups['webservers']
判断变量是否不在某个列表里
- hosts: all
tasks:
- shell: 'reboot now'
#如果不是admin组的主机就直接重启
when: ansible_hostname not in groups['admin']
比较运算符 | 逻辑运算符 | ||
---|---|---|---|
== | 等于 | and | 与 |
!= | 不等于 | or | 或 |
大于 | not | 非 | |
< | 小于 | () | 组合 |
| 大于等于 | and | 与 |
<= | 小于等于 | or | 或 |
操作系统是Redhat7的6版本或者7版本并且主机名不是host2
- hosts: all
remote_user: root
tasks:
- name: compare os version
debug:
msg: "System is RedHat6 or Redhat7 And host not host2"
when: ansible_distribution == "RedHat" and (ansible_distribution_major_version == "6" or ansible_distribution_major_version == "7") and (not ansible_nodename == "host2")
defined | 变量已经定义 |
---|---|
undefined | 变量未定义 |
none | 变量定义了,没有值 |
[student@workstation ansible]$ cat var_define.yml
- hosts: servera
gather_facts: no
vars:
keys:
tasks:
- debug:
msg: "ansible_hostname is not defined"
when: ansible_hostname is undefined
- debug:
msg: "key is defined"
when: keys is defined
- debug:
msg: "keys is none"
when: keys is none
succeeded | 通过任务返回信息,执行成功返回真 |
---|---|
failed | 通过任务返回信息,执行失败返回真 |
change | 通过任务返回信息,执行状态为change返回真 |
Skipped | 通过任务返回信息,任务没有满足条件跳过执行,返回真 |
[student@workstation ansible]$ cat status.yml
- hosts: servera
gather_facts: no
tasks:
- shell: ls /home/
register: retmsg
- debug:
msg:
- "runing successfully"
- " {{ retmsg.stdout }}"
when: retmsg is succeeded
- debug:
msg:
- "runing successfully"
- " {{ retmsg.stdout }}"
when: retmsg is failed
- debug:
msg:
- "runing successfully"
- " {{ retmsg.stdout }}"
when: retmsg is change
- debug:
msg:
- "runing successfully"
- " {{ retmsg.stdout }}"
when: retmsg is skipped
string | 全部是字符串返回真 |
---|---|
lower | 字符串全部小写返回真 |
upper | 字符串全部大写返回真 |
even | 偶数返回真 |
odd | 奇数返回真 |
subset | 一个list是另一个的子集返回真 |
superset | 一个list是另一个的父集返回真 |
number | 全部是数字返回真 |
[student@workstation ansible]$ cat when_other.yml
- hosts: servera
gather_facts: no
vars:
comment: "wolala wolala wolalala"
age: 13
tasks:
- debug:
msg: "This is string"
when: comment is string
- debug:
msg: "This is lower"
when: comment is lower
- debug:
msg: "This not upper"
when: comment is not upper
- debug:
msg: "This is not even"
when: age is not even
- debug:
msg: "This is odd"
when: age is odd
- debug:
msg: "This is number"
when: age is number
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。