32421312 发表于 2016-3-29 09:31:59

Ansible9:条件语句


    在有的时候play的结果依赖于变量、fact或者是前一个任务的执行结果,从而需要使用到条件语句。
一、when
    有的时候在特定的主机需要跳过特定的步骤,例如在安装包的时候,需要指定主机的操作系统类型,或者是当操作系统的硬盘满了之后,需要清空文件等,可以使用when语句来做判断 。when关键字后面跟着的是python的表达式,在表达式中你能够使用任何的变量或者fact,当表达式的结果返回的是false,便会跳过本次的任务

1、基本用法,示例:

---
- name: Install VIM
hosts: alltasks:
    - 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模块,这样可以让用户自己决定是否继续运行任务:

- name: pause for unexpected conditions
pause: prompt="Unexpected OS"
when: ansible_os_family !="RedHat"

2、在when中使用jinja2的语法,示例:

tasks:
- command: /bin/false
    register: result      #将命令执行的结果传递给result变量
    ignore_errors: True    #忽略错误
- command: /bin/something
    when: result|failed    #判断result中是否有failed字符串,如果有,则执行指定的指令
- command: /bin/something_else
    when: result|success    #如果result中包含success字符串,则条件成立
- command: /bin/still/something_else
    when: result|skipped3、使用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

4、如果变量不存在,则可以通过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

5、when在循环语句中的使用方法,示例:

tasks:
    - command: echo {{ item }}
      with_items: [ 0, 2, 4, 6, 8, 10 ]
      when: item > 56、在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' }

二、条件导入

有些时候,你也许想在一个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

有些时候,我们想基于不同的操作系统,选择不同的配置文件,及配置文件的存放路径,可以借助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/

四、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"五、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状态



页: [1]
查看完整版本: Ansible9:条件语句