When 语句
有时候用户有可能需要某一个主机越过某一个特定的步骤.这个过程就可以简单的像在某一个特定版本的系统上 少装了一个包一样或者像在一个满了的文件系统上执行清理操作一样. 这些操作在Ansible上,若使用when
语句都异常简单.When语句包含Jinja2表达式 实际上真的很简单:
tasks:
- name: "yum install lrzsz"
command: /sbin/shutdown -t now
when: ansible_os_family == "Centos"
解释
当系统是centos的时候才会执行上面的command,否则是不会执行的
系统类型: **ansible docker -m setup ** 来查看
例子:
root@centos-mysql01:~# ansible docker -m setup|grep os_family
"ansible_os_family": "RedHat",
"ansible_os_family": "Debian",
文件判断:
tasks:
- command: /bin/false
register: result
ignore_errors: True
- command: /bin/something
when: result|failed
- command: /bin/something_else
when: result|success
- command: /bin/still/something_else
when: result|skipped
解释
ignore_errors: True :代表此task出错是忽略报错,继续执行下面的task,否则此task报错,不在执行后续的task
当第一个command返回值为failed
第二个在返回值为failed的时候执行,否则执行第二个,当为命令为skipped的时候执行第三个
或者使用vars来实现
vars:
epic: true
一个条件选择执行也许看起来像这样:
tasks:
- shell: echo "This certainly is epic!"
when: epic
或者像这样:
tasks:
- shell: echo "This certainly isn't epic!"
when: not epic
如果一个变量不存在,你可以使用Jinja2的`defined`命令跳过或略过.例如:
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
例子:
- hosts: docker
remote_user: root
vars:
epic: true
tasks:
- name: file exeit
shell: ls /home/sh
register: result
ignore_errors: True
- file: path=/home/sh state=directory mode=644
when: result|failed #如果文件不存在 会创建
- file: path=/opt/sh state=directory mode=644
when: epic
#我现在是/opt/sh目录不存在,/home/sh目录存在
root@centos-mysql01:/data/sh# ls /opt/sh
ls: 无法访问/opt/sh: 没有那个文件或目录
root@centos-mysql01:/data/sh# ls /home/sh/
![](https://s1.51cto.com/images/blog/201812/13/ca5b8c55aa74a71eb2d4e7e3c9f08b12.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
剩下的失败和skipped需要自己去实现了
note当同时使用when
和with_items
句对于不同项目将会单独处理
tasks:
- command: echo {{ item }}
with_items: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
应用于role里面
- hosts: webservers
roles:
- { role: debian_stock_config, when: ansible_os_family == 'Debian' }
ansible运算符
比较运算符
== 表示对象是否等值 等于为真
!= 表示对象是否不等值 不等于为真
> 比较对象值的大小 左边大于右边为真
>= 比较对象值的大小 左边大于或者等于右边为真
< 比较对象值的大小 左边小于右边为真
<= 比较对象值的大小 左边小于或者等于右边为真
逻辑运算符
and 逻辑与 当左右两边的值为真 返回真
or 逻辑或 当左右两边有一边的值为真 返回真
not 取反 对操作取反值
() 组合 将一组集合在一起形成一个操作
- hosts: docker
remote_user: root
tasks:
- debug:
msg: "System release is centos7"
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"
等同于
- hosts: dcoker
remote_user: root
tasks:
- debug:
msg: "System release is centos7"
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
#### ()组合操作
- hosts: docker
remote_user: root
tasks:
- debug:
msg: "System release is centos6 or centos7"
when: ansible_distribution == "CentOS" and
(ansible_distribution_major_version == "6" or ansible_distribution_major_version == "7")
== 和!=操作
tasks:
- name: task1
shell: "ls /data/soft
register: result
ignore_errors: true
- name:
when: result.rc == 0
- name: task3
when: result.rc != 0
testc来判断
- hosts: docker
remote_user: root
vars:
testpath: /data/sh
tasks:
- name: file exists
shell: mkdiri -p {{testpath}}
when: not testpath is exists
判断testc,直接可以使用 is exists判断,如果存在,使用not is exists判断不存在
或者使用:
defined 判断变量是否定义 定义为真
undefined 判断变量是否定义 未定义为真
none 判断变量是否为空 为空为真
fine 是否为文件
directory 是否为目录
exists 是否存在
link 是否为链接文件
- hosts: docker
remote_user: root
vars:
testpath: /data/sh
testfile: "/data/sh1"
testdir: "/data/sh2"
testlink: "/data/sh3"
testexists: "data/sh4"
tasks:- name: file exists
shell: mkdiri -p {{testpath}}
when: not testpath is exists - debug:
msg: "file"
when: testfile is file - debug:
msg: "directory"
when: testdir is directory - debug:
msg: "exists"
when: testexists is exists
- name: file exists