When 条件判断

发布时间: 更新时间: 总字数:1596 阅读时间:4m 作者: IP上海 分享 网址

ansible条件判断的关键字when,条件成立,则执行任务;条件不成立,则不执行任务

介绍

  • when 关键字中引用变量时,变量名不需要加 {{ }}
  • 比较运算符:==!=><>=<=
  • 逻辑运算符
    • and 逻辑与
    • or 逻辑或
    • not 取反
    • () 组合,将一组操作体包装为一个整体

demo1

---
- hosts: h-1
  remote_user: root
  tasks:
  - debug:
      msg: "System release is Ubuntu"
    when: ansible_distribution == "Ubuntu"
    # when:
    # - ansible_distribution == "Ubuntu"
    # - ansible_distribution_major_version == "20"
    # when: xxx.rc == 0

执行结果

ansible-playbook -i hosts.test when-demo1.yaml ...
root@dev:~# ansible-playbook -i hosts.test when-demo1.yaml

PLAY [h-1] ***********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [h-1]

TASK [debug] *********************************************************************************************************
ok: [h-1] => {
    "msg": "System release is Ubuntu"
}

PLAY RECAP ***********************************************************************************************************

demo2

---
- hosts: h-1
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_items:
    - 1
    - 2
    - 3
    when: item > 1
ansible-playbook -i hosts.test when-demo2.yaml ...
root@dev:~# ansible-playbook -i hosts.test when-demo2.yaml

PLAY [h-1] ***********************************************************************************************************

TASK [debug] *********************************************************************************************************
skipping: [h-1] => (item=1)
ok: [h-1] => (item=2) => {
    "msg": 2
}
ok: [h-1] => (item=3) => {
    "msg": 3
}

PLAY RECAP ***********************************************************************************************************
h-1                        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

block

block 用来解决条件成立时,执行block中的多个任务

---
- hosts: h-1
  remote_user: root
  tasks:
  - debug:
      msg: "task1 not in block"
  - block:
      - debug:
          msg: "task2 in block1"
      - debug:
          msg: "task3 in block2"
    when: 2 > 1

block 的错误处理功能解决当某任务出错时,执行指定的其他任务

---
- hosts: h-1
  remote_user: root
  tasks:
  - shell: 'ls /ooo'
    register: return_value
    ignore_errors: true
  - debug:
      msg: "i caught an error"
    when: return_value is failed

rescue

rescue 意思为 救援,表示当 block 中的任务执行失败时,会执行 rescue 中的任务进行补救

---
- hosts: h-1
  remote_user: root
  tasks:
  - block:
    - shell: 'ls /ooo'
    - shell: 'ls /testdir'
    - shell: 'ls /a'
    rescue:
    - debug:
        msg: "i caught an error1"
    - debug:
        msg: "i caught an error2"
    always:
    - debug:
        msg: "This always executes"

always

always 表示无论 block 中的任务执行成功还是失败,always 中的任务都会被执行

tests

  • 类似于 Linux 的 test 命令
  • tests 的判断均针对于 ansible 主机中的路径,与目标主机无关

判断是否存在

  • exists 判断路径是否存在
  • file 判断路径是否是一个文件
  • directory 判断路径是否是一个目录
  • link :判断路径是否是一个软链接
  • mount 判断路径是否是一个挂载点
  • defined 判断变量是否已经定义,已经定义则返回真
  • undefind 判断变量是否已经定义,未定义则返回真
  • none 判断变量值是否为空,如果变量已经定义,但是变量值为空,则返回真
---
- hosts: h-1
  remote_user: root
  gather_facts: no
  vars:
    testpath1: "/testdir/test"
    testpath2: "/testdir/"
    testpath3: "/testdir/testsoftlink"
    testpath4: "/testdir/testhardlink"
    testpath5: "/boot"
    testvar1:
  tasks:
  - debug:
      msg: "file exist"
    when: testpath1 is exists
    # when: testpath is not exists
    # when: testvar2 is undefined
    # when: testvar1 is none
  - debug:
      msg: "file"
    when: testpath1 is file
  - debug:
      msg: "directory"
    when: testpath2 is directory
  - debug:
      msg: "link"
    when: testpath3 is link
  - debug:
      msg: "link"
    when: testpath4 is link
  - debug:
      msg: "mount"
    when: testpath5 is mount

判断执行结果

  • successsucceeded 任务执行成功则返回真
  • failurefailed 任务执行失败则返回真
  • changechanged 任务执行状态为changed则返回真
  • skipskipped 任务跳过
---
- hosts: h-1
  remote_user: root
  gather_facts: no
  vars:
    doshell: "yes"
  tasks:
  - shell: "cat /etc/hosts"
    when: doshell == "yes"
    register: returnmsg
    ignore_errors: true
  - debug:
      msg: "success"
    when: returnmsg is success
  - debug:
      msg: "failed"
    when: returnmsg is failure
  - debug:
      msg: "changed"
    when: returnmsg is change
  - debug:
      msg: "skip"
    when: returnmsg is skip
ansible-playbook -i hosts.test when-demo4.yaml ...
root@dev:~# ansible-playbook -i hosts.test when-demo4.yaml

PLAY [h-1] ***********************************************************************************************************

TASK [shell] *********************************************************************************************************
changed: [h-1]

TASK [debug] *********************************************************************************************************
ok: [h-1] => {
    "msg": "success"
}

TASK [debug] *********************************************************************************************************
skipping: [h-1]

TASK [debug] *********************************************************************************************************
ok: [h-1] => {
    "msg": "changed"
}

TASK [debug] *********************************************************************************************************
skipping: [h-1]

PLAY RECAP ***********************************************************************************************************
h-1                        : ok=3    changed=1    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

判断字符串

  • lower 判断包含字母的字符串中的字母是否是纯小写,字符串中的字母全部为小写则返回真
  • upper 判断包含字母的字符串中的字母是否是纯大写,字符串中的字母全部为大写则返回真
  • string 判断对象是否是一个字符串,是字符串则返回真
  • number 判断对象是否是一个数字,是数字则返回真

判断整除

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

判断版本号

  • version 可以用于对比两个版本号的大小
---
- hosts: h-1
  remote_user: root
  vars:
    ver: 7.4.1708
    ver1: 7.4.1707
  tasks:
  - debug:
      msg: "This message can be displayed when the ver is greater than ver1"
    when: ver is version(ver1,">")
  - debug:
      msg: "system version {{ansible_distribution_version}} greater than 7.2"
    when: ansible_distribution_version is version("7.2","gt")

判断是否子集或父集

  • subset 判断一个list是不是另一个list的子集,是另一个list的子集时返回真
  • superset 判断一个list是不是另一个list的父集,是另一个list的父集时返回真
- hosts: h-1
  remote_user: root
  gather_facts: no
  vars:
    a:
    - 2
    - 5
    b: [1,2,3,4,5]
  tasks:
  - debug:
      msg: "A is a subset of B"
    when: a is subset(b)
  - debug:
      msg: "B is the parent set of A"
    when: b is superset(a)

fail

  • fail 模块天生就是一个用来 执行失败 的模块
    • 当fail模块执行后,playbook就会认为有任务失败
---
- hosts: h-1
  remote_user: root
  tasks:
  - debug:
      msg: "1"
  - shell: "echo 'This is a string for testing--error'"
    register: return_value
  - fail:
  # - fail:
  #     msg: "Interrupt running playbook"
  - fail:
      msg: "Interrupt running playbook"
    when: "'error' in return_value.stdout"
  - debug:
      msg: "2"

failed_when

  • failed_when 当对应的条件成立时,将对应任务的执行状态设置为失败
---
- hosts: h-1
  remote_user: root
  tasks:
  - debug:
      msg: "I execute normally"
  - shell: "echo 'This is a string for testing error'"
    register: return_value
    failed_when: ' "error" in return_value.stdout'
  - debug:
      msg: "I never execute,Because the playbook has stopped"

changed_when

  • changed_when 关键字的作用是在条件成立时,将对应任务的执行状态设置为changed
  • changed_when: false 让对应的任务永远不能是changed状态
---
- hosts: h-1
  remote_user: root
  tasks:
  - debug:
      msg: "test message"
    changed_when: 2 > 1
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数