ansible6:ansible条件判断


when

  1. 直接举例(此处牵扯到with_item循环可在第8节查看)。

      0 18:10:49 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test1.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "{{item}}"
        with_items: [1,2,5,52]
        when: item > 2
      0 18:10:50 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test1.yaml
    
  2. ansible运算符:

    • 比较运算符:==、!=、>、>=、<、<=、
    • 逻辑运算符:and、or、not
      0 18:14:33 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test2.yaml
    ---
    - hosts: ck-node1
      tasks:
      - name: task1
        shell: "ls -l /wula"
        register: return_info
        ignore_errors: true		# 忽略报错,继续往下执行。
      - name: task2
        debug:
          msg: "Command execution successful"
        when: return_info.rc == 0		# task1任务的返回值。
      - name: task3
        debug:
          msg: "Command execution failed"
        when: return_info.rc != 0
      0 18:14:37 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test2.yaml
    

tests

  jinja2模板中的tests是一类判断的统称,下面一些test判断均针对于ansible控制主机,与目标主机无关。每一种判断都有两种情况:is和is not,下面简单举例说明。

判断路径

  1. exists:判断文件是否存在。

    • is exists:文件存在则为真。
    • is not exists:文件不存在则为真。
      0 18:44:02 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test3.yaml 
    ---
    - hosts: ck-node1
      vars:
        testdir: /root/temp
      tasks:
      - debug:
          msg: "file exist"
        when: "testdir is exists"
      0 18:44:06 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test3.yaml
    
  2. file:判断路径是否是一个文件,如果路径是一个文件则返回真。

  3. directory:判断路径是否是一个目录,如果路径是一个目录则返回真。

  4. link:判断路径是否是一个软链接,如果路径是一个软链接则返回真

  5. mount:判断路径是否是一个挂载点,如果路径是一个挂载点则返回真

判断变量

  1. defined:判断变量是否已经定义,已经定义则为真。
  2. undefined:判断变量是否已经定义,未定义则返回真。
  3. none:判断变量值是否为空,如果变量已定义且值为空,则返回真。

判断执行结果

  1. success或succeeded:通过任务的返回信息判断任务的执行状态,任务执行成功,则返回真。
  2. failure 或 failed:通过任务的返回信息判断任务的执行状态,任务执行失败,则返回真。
  3. change 或 changed:通过任务的返回信息判断任务的执行状态,任务执行状态为changed,则返回真。
  4. skip 或 skipped:通过任务的返回信息判断任务的执行状态,当任务没有满足条件,而被跳过执行时,则返回真。

判断字符串

  1. lower:判断包含字母的字符串中的字母是否是纯小写,字符串中的字母全部为小写则返回真
  2. upper:判断包含字母的字符串中的字母是否是纯大写,字符串中的字母全部为大写则返回真
  3. string:判断对象是否是一个字符串,是字符串则返回真

判断整除

  1. even :判断数值是否是偶数,是偶数则返回真
  2. odd :判断数值是否是奇数,是奇数则返回真
  3. divisibleby(num) :判断是否可以整除指定的数值,如果除以指定的值以后余数为0,则返回真

其它判断

  1. number:判断对象是否是一个数字,是数字则返回真。

      0 18:45:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test4.yaml
    ---
    - hosts: ck-node1
      vars:
        var1: 1
        var2: "1"
        var3: 0.1
      tasks:
      - name: task1
        debug:
          msg: "this variable is number"
        when: var1 is number
      - name: task2
        debug:
          msg: "this variable is number"
        when: var2 is number
      - name: task3
        debug:
          msg: "this variable is number"
        when: var3 is number
      0 18:45:54 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test4.yaml
    

其它说明

block

  1. 使用when判断,条件成立后只能执行一个任务,想要在条件成立后执行多个任务就需要用到block块。

      0 19:10:35 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test5.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "task1 not in block"
      - block:
        - debug:
            msg: "task2 in block"
        - debug:
            msg: "task3 in block"
        when: 2 > 1
      0 19:10:37 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test5.yaml
    
  2. 使用block+rescue实现错误判断。

    # 学block之前可以使用tests的faild或者success方法来判断。
      0 19:16:19 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test6.yaml
    ---
    - hosts: ck-node1
      tasks:
      - shell: "ls -l /root/wula"
        register: return_info
        ignore_errors: true
      - debug:
          msg: "command exec failed"
        when: return_info is failed
      0 19:16:29 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test6.yaml
    # 现在可以使用block实现错误判断(此种方式会自动忽略错误继续执行,不用再加“ignore_errors: true”)。
      0 19:19:11 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test7.yaml
    ---
    - hosts: ck-node1
      tasks:
      - block:
        - shell: "ls -l /root/wula"
        rescue:
        - debug:
            msg: "command exec failed"
      0 19:19:12 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test7.yaml
    

错误判断

  1. 在shell中,判断某个条件成立后,立即中止脚本的运行可以使用exit,在ansible中想要实现此功能则需要借助fail模块。默认情况下,playbook执行过程中出错会自动中止,除非设置了ignore_errors: true,fail模块就可以实现“执行失败”的场景。

      0 19:22:36 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test8.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "1"
      - debug:
          msg: "2"
      - fail:
          msg: "interrupt running playbook"		# “msg”用来自定义错误输出信息,可以不定义,默认是“Failed as requested from task”
      - debug:
          msg: "3"
      0 19:22:38 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test8.yaml
    
  2. 通常不会无缘无故想要中止playbook,fail一般与when连用,当触发某种条件时中断playbook。

      0 19:29:26 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test9.yaml
    ---
    - hosts: ck-node1
      tasks:
      - shell: "echo error"
        register: return_info
      - fail:
          msg: "commad exec failed"        
        when: "'error' in return_info.stdout" 
      - debug:
          msg: "how are you"
      0 19:29:29 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test9.yaml
    
  3. failed_when:在条件成立时,将对应任务的执行状态设置为FAILED。跟fail+when的作用类似。

      0 19:33:33 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test10.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "wula"
      - shell: "echo error"
        register: return_info
        failed_when: "'error' in return_info.stdout" 
      - debug:
          msg: "how are you"
      0 19:33:35 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test10.yaml
    
  4. changed_when:在条件成立时,将对应任务的执行状态设置为changed。可以将下面列子中的changed_when改成when对比一下,会发现when对应的是绿色的“OK”状态”,changend_when对应的是黄色的“changed”状态。

      0 19:36:18 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test11.yaml
    ---
    - hosts: ck-node1
      tasks:
      - debug:
          msg: "test message"
        changed_when: 2 > 1
      0 19:36:20 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test11.yaml
    


写作不易,转载请注明出处,谢谢~~

原文地址:https://www.cnblogs.com/ccbloom/p/15508585.html