首页 > 其他 > 详细

Ansible14:Playbook条件语句

时间:2019-06-10 13:50:14      阅读:155      评论:0      收藏:0      [点我收藏+]

简介

在有的时候play的结果依赖于变量、fact或者是前一个任务的执行结果,或者有的时候,我们会基于上一个task执行返回的结果而决定如何执行后续的task。这个时候就需要用到条件判断。

条件语句在Ansible中的使用场景:

  • 在目标主机上定义了一个硬限制,比如目标主机的最小内存必须达到多少,才能执行该task
  • 捕获一个命令的输出,根据命令输出结果的不同以触发不同的task
  • 根据不同目标主机的facts,以定义不同的task
  • 根据目标机的cpu的大小,以调优相关应用性能
  • 用于判断某个服务的配置文件是否发生变更,以确定是否需要重启服务

when关键字

1. when基本使用

在ansible中,使用条件判断的关键字就是when。

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

下面是一个基本的用法示例:

---
- 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"

也可以使用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

2. 条件语句表达式

表达式 示例
字条串相等 ansible_machine == "x86_64"
数字相等 max_memory == 512
小于 min_memory < 512
大于 min_memory > 256
小于等于 min_memory <=256
大于等于 min_memory >= 256
不等于 min_memory != 256
变量存在 min_memory is defined
变量不存在 min_memory is not defined
布尔值: 1, True, yes为真;0,False, no为假 memory_available
布尔值为假 not meory_available
第一个变量的值是否在第二个变量的列表当中 ansible_distribution in supported_distros

通过when判断变量是否存在:

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

3. 在循环语句中使用条件语句

# 只打印大于5的值
tasks:
    - command: echo {{ item }}
      loop: [ 0, 2, 4, 6, 8, 10 ]
      when: item > 5
# 确保将mariadb-server安装到根分区且根分区的可用空间要大于300M
- name: install mariadb-server if enough space on root
  yum: 
    name: mariadb-server
    state;拉特st
  loop: "{{ ansible_mounts }}"
  when: item.mount == "/" and item.size_available > 300000000

4. 在注册变量中使用条件语句

- name: restart httpd if postfix is running
  hosts: test
  tasks:
    - name: get postfix server status
      command: /usr/bin/systemctl is-active postfix
      ignore_errors: yes
      register: result
      
    - name: restart apache httpd based on postfix status
      service:
        name: httpd
        state: restarted
      when: result.rc == 0

5. 多条件判断

在Ansible中,多条件判断可使用and或者or来连接多条语句:

or示例:

when: ansible_distribution == "RedHat" or ansible_distribution == "Fedora"

and示例:

when: ansible_distribution_version == "7.5" and ansible_kernel == "3.10.0-327.el7.x86_64"

and的条件语句也可以直接写成如下形式:
when: - ansible_distribution_version == "7.5" - ansible_kernel == "3.10.0-327.el7.x86_64"

更复杂的条件语句示例:

when: => 
  ( ansible_distribution == "RedHat" and ansible_distribution_major_version == "7" )
  or
  ( ansible_distribution == "Fedora" and ansible_distribution_major_version == "28")

条件导入

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

# 将paths中的路径与files中的文件名拼接成一个完整的文件路径,在被控端找到哪个就优先使用哪个:
- name: template a file
   template: src={{ item }} dest=/etc/myapp/foo.conf
   with_first_found:
     - files:
        - httpd.conf
        - apache2.conf
       paths:
        - /etc/httpd/conf/
        - /usr/local/apache2/conf/

Ansible14:Playbook条件语句

原文:https://www.cnblogs.com/breezey/p/10996632.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!