zoukankan      html  css  js  c++  java
  • Ansible When有条件的运行任务

    When 条件表达式

    •   ==  <  >  <=  >=  !=  or  and
    •   vars is defined      # 变量存在
    •   vars is not defined    # 变量不存在
    •   vars in list            # 变量在列表中

    When 四种register结果判断

    •     when: result is failed      #执行失败
    •     when: result is success  #执行成功
    •     when: result is skipped  #被跳过执行
    •     when: result is changed #是否修改

    示例1 bool判断

    ---
    - name: when test
      hosts: dev
      vars:
        run_debug: true
      tasks:
        - name: debug
          debug:
            msg: "hello world"
          when: run_debug |bool

    示例2 defined

    ---
    - name: when test
      hosts: dev
      vars:
        run_service: firewalld
      tasks:
        - name: "{{ run_service}} service"
          service:
            name: "{{ run_service }}"
            state: started
          when: run_service is defined

    示例3 in list

    ---
    - name: when test
      hosts: dev
      vars:
        support_os:
        - RedHat
        - CentOS
      tasks:
        - name: "OS Type"
          debug:
                  msg: "The os type is {{ ansible_distribution }}"
          when: ansible_distribution in support_os  # ansible_distribution是ansible_facts中的变量

    示例4 when + register

    ---
    - name: when test
      hosts: dev
      tasks:
        - name: start chronyd service
          service:
            name: chronyd
            state: started
          ignore_errors: true
          register: result- name: debug
          debug:
            msg: "{{ result.failed }}"
          when: result is success  # 也可以使用 result.failed == false

    示例4 loop + when (将对Loop中每项都执行when)

    ---
    - name: when test
      hosts: dev
      tasks:
        - name: debug
          debug:
              msg: "{{ item }}"
          loop: "{{ ansible_mounts }}"
          when: item.mount == "/" and item.size_available > 300000000
  • 相关阅读:
    内存泄漏检测
    qt 关于内存泄漏的检测
    Valgrind 安装与使用
    Qt应用中检测内存泄露——VLD
    Visual C++内存泄露检测—VLD工具使用说明
    ArcGIS Runtime支持的GP工具列表(转 )
    c# 调用ArcEngine的GP工具
    ArcEngine 数据导入经验(转载)
    在ArcEngine中使用Geoprocessing工具-执行工具
    利用C#与AE调用GP工具
  • 原文地址:https://www.cnblogs.com/vincenshen/p/12611783.html
Copyright © 2011-2022 走看看