一、基本用法,示例:python
---
- 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"
条件语句还有一种用法,它还可让你当达到必定的条件的时候暂停下来,等待你的输入确认。通常状况下,当ansible遭遇到error时,它会直接结束运行。那其实你能够当遭遇到不是预期的状况的时候给使用pause模块,这样可让用户本身决定是否继续运行任务:web
- name: pause for unexpected conditions
pause: prompt="Unexpected OS"
when: ansible_os_family !="RedHat"
二、在when中使用jinja2的语法,示例:
tasks: - command: /bin/false register: result #将命令执行的结果传递给result变量 ignore_errors: True #忽略错误 - command: /bin/something when: result|failed #若是注册变量的值 是任务failed则返回true - command: /bin/something_else when: result|success #若是注册变量的值是任务success则返回true - command: /bin/still/something_else when: result|skipped #若是注册变量的值是任务skipped则返回true - command: /bin/foo when: result|changed #若是注册变量的值是任务changed则返回true
- 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
四、若是变量不存在,则能够经过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
五、when在循环语句中的使用方法,示例:
tasks: - command: echo {{ item }} with_items: [ 0, 2, 4, 6, 8, 10 ] when: item > 5六、在include和roles中使用when:
在include中使用的示例:- include: tasks/sometasks.yml when: "'reticulating splines' in output"
在roles中使用的示例:- hosts: webservers roles: - { role: debian_stock_config, when: ansible_os_family == 'Debian' }
2、条件导入
有些时候,你也许想在一个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’。 shell
3、with_first_found
有些时候,咱们想基于不一样的操做系统,选择不一样的配置文件,及配置文件的存放路径,能够借助with_first_found来解决:
- name: template a file template: src={{ item }} dest=/etc/myapp/foo.conf with_first_found: - files: - {{ ansible_distribution }}.conf - default.conf paths: - search_location_one/somedir/ - /opt/other_location/somedir/
4、failed_when
failed_when实际上是ansible的一种错误处理机制,是由fail模块使用了when条件语句的组合效果。示例以下:
- name: this command prints FAILED when it fails command: /usr/bin/example-command -x -y -z register: command_result failed_when: "'FAILED' in command_result.stderr"咱们也能够直接经过fail模块和when条件语句,写成以下:
- name: this command prints FAILED when it fails command: /usr/bin/example-command -x -y -z register: command_result ignore_errors: True - name: fail the play if the previous command did not succeed fail: msg="the command failed" when: "'FAILED' in command_result.stderr"5、changed_when
当咱们控制一些远程主机执行某些任务时,当任务在远程主机上成功执行,状态发生更改时,会返回changed状态响应,状态未发生更改时,会返回OK状态响应,当任务被跳过期,会返回skipped状态响应。咱们能够经过changed_when来手动更改changed响应状态。示例以下:
- shell: /usr/bin/billybass --mode="take me to the river" register: bass_result changed_when: "bass_result.rc != 2" #只有该条task执行之后,bass_result.rc的值不为2时,才会返回changed状态 # this will never report 'changed' status - shell: wall 'beep' changed_when: False #当changed_when为false时,该条task在执行之后,永远不会返回changed状态
本文出自 “无名小卒” 博客,请务必保留此出处http://breezey.blog.51cto.com/2400275/1757593apache