自动化运维—playbook(判断)


文章目录

  • 自动化运维---playbook(判断)
  • playbook1
  • playbook2
  • playbook3
  • playbook4
  • playbook5
  • playbook6
  • playbook7
  • playbook8
  • playbook9
  • playbook10
  • playbook11
  • playbook12
  • playbook13


playbook1

when关键字:
绝大多数语言中,都使用if作为条件判断的关键字,而在ansible中,条件判断的关键字是when,我们可以使用when关键字为任务指定条件,条件成立,则执行任务,条件不成立,则不执行任务

vim pd1.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_items:
    - 1
    - 2
    - 3
    when: item > 1

表示item大于1时才输出

playbook 判断result ansible playbook 判断_playbook 判断result

playbook2

在ansible中,我们可以使用如下比较运算符

== :比较两个对象是否相等,相等为真

!= :比较两个对象是否不等,不等为真

> : 比较两个值的大小,如果左边的值大于右边的值,则为真

< :比较两个值的大小,如果左边的值小于右边的值,则为真

>= :比较两个值的大小,如果左边的值大于右边的值或左右相等,则为真

<= :比较两个值的大小, 如果左边的值小于右边的值或左右相等,则为真

判断远程主机版本:
首先我们可以用一条命令查看远程主机的的版本信息:

ansible testB -m setup | grep ansible_distribution

playbook 判断result ansible playbook 判断_执行状态_02

vim pd2.yml
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "System release is redhat7"
    when: ansible_distribution == "RedHat" and ansible_distribution_major_version == "7"

playbook 判断result ansible playbook 判断_执行状态_03


写法2:

vim pd2.yml
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "System release is redhat7"
    when:
    - ansible_distribution == "RedHat"
    - ansible_distribution_major_version == "7"

写法3:多个判断信息组合

可用的逻辑运算符如下

and :逻辑与,当左边与右边同时为真,则返回真
or :逻辑或,当左边与右边有任意一个为真,则返回真
not :取反,对一个操作体取反
( ) :组合,将一组操作体包装在一起,形成一个较大的操作体
vim pd2.yml
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "System release is redhat7 or redhat6"
    when: ansible_distribution == "RedHat" and (ansible_distribution_major_version == "7" or ansible_distribution_major_version == "6")

playbook 判断result ansible playbook 判断_执行状态_04

写法4:否定判断

vim pd2.yml
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "System release is not windows"
    when: not ansible_distribution == "Windows"

playbook 判断result ansible playbook 判断_vim_05

playbook3

通过返回值判断执行情况:
什么是返回值?有点像shell中的echo $?
我们通过一个剧本举例说明:

vim pd3.yml
---
- hosts: testB
  remote_user: root
  tasks:
  - name: task1
    shell: "date"
    register: returnmsg
  - name: task2
    debug:
      var: returnmsg

我们可以看到,当task1正常执行时,返回的结果rc为0

playbook 判断result ansible playbook 判断_vim_06

修改内容,让它无法正常执行:

vim pd3.yml
---
- hosts: testB
  remote_user: root
  tasks:
  - name: task1
    shell: "dateaaaa"
    register: returnmsg
  - name: task2
    debug:
      var: returnmsg

我们会看到报错,task1无法正常执行而报错,而此时task2就无法执行,所以我们需要借助一个参数来忽略此错误

playbook 判断result ansible playbook 判断_字符串_07

修改内容,忽略错误:

vim pd3.yml
---
- hosts: testB
  remote_user: root
  tasks:
  - name: task1
    shell: "dateaaaa"
    register: returnmsg
    ignore_errors: true
  - name: task2
    debug:
      var: returnmsg

此时我们可以看到当task2无法正常执行时,rc为127(不为0)

playbook 判断result ansible playbook 判断_vim_08

playbook4

通过返回值判断目录是否存在:

vim pd4.yml
---
- hosts: testB
  remote_user: root
  tasks:
  - name: task1
    shell: "ls /testttt"
    register: returnmsg
    ignore_errors: true
  - name: task2
    debug:
      msg: "Command execution successful"
    when: returnmsg.rc == 0
  - name: task3
    debug:
      msg: "Command execution failed"
    when: returnmsg.rc != 0

在远程主机中查看到没有该目录:

playbook 判断result ansible playbook 判断_字符串_09


返回Command execution failed信息:

playbook 判断result ansible playbook 判断_字符串_10

playbook5

test判断:我们在shell中经常会用test来判断:

在命令行中通过test -e 来判断目录文件是否存在,并且通过查看返回值来查看结果

playbook 判断result ansible playbook 判断_vim_11


在脚本中:如果符合判断条件返回true 不符合返回false

playbook 判断result ansible playbook 判断_字符串_12

vim pd5.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testpath: /testdir
  tasks:
  - debug:
      msg: "file exist"
    when: testpath is exists

注意:此处判断的是本地主机,与目标远程主机无关

playbook 判断result ansible playbook 判断_字符串_13


写法2:判断不存在

vim pd5.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testpath: /testdirhaha
  tasks:
  - debug:
      msg: "file not exist"
    when: testpath is not exists

playbook 判断result ansible playbook 判断_执行状态_14


写法3:否定not可以写在前面

vim pd5.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testpath: /testdirhaha
  tasks:
  - debug:
      msg: "file not exist"
    when: not testpath is exists

playbook6

判断变量:

判断变量的一些tests
defined : 判断变量是否已经定义,已经定义则返回真
undefind : 判断变量是否已经定义,未定义则返回真
none : 判断变量值是否为空,如果变量已经定义,但是变值为空,则返回真
vim pd6.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testvar: "test"
    testvar1:
  tasks:
  - debug:
      msg: "variable is defined"
    when: testvar is defined
  - debug:
      msg: "variable is undefined"
    when: testvar2 is undefined
  - debug:
      msg: "variable is defined but there is no value"
    when: testvar1 is none

playbook 判断result ansible playbook 判断_playbook 判断result_15

playbook7

判断执行结果:

判断执行结果的一些tests:

success或succeeded:通过任务的返回信息判断任务的执行状态,任务执行成功则返回真

failure或failed:通过任务的返回信息判断任务的执行状态,任务执行失败则返回真

change或changed:通过任务的返回信息判断任务的执行状态,任务执行状态为changed则返回真

skip或skipped:通过任务的返回信息判断任务的执行状态,当任务没有满足条件,而被跳过执行时,则返回真
vim pd7.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    doshell: "yes"
  tasks:
  - shell: "cat /testdir/123"
    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 skipped

playbook 判断result ansible playbook 判断_字符串_16

playbook8

判断路径:

判断路径的一些tests:

注:如下tests的判断均针对于ansible主机中的路径,与目标主机无关

file : 判断路径是否是一个文件,如果路径是一个文件则返回真

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

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

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

exists: 判断路径是否存在,如果路径存在则返回真!
vim pd8.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testpath1: "/testdir/"
    testpath2: "/testdir/test"
    testpath3: "/testdir/testsoftlink"
    testpath4: "/testdir/testhardlink"
    testpath5: "/boot"
  tasks:
  - debug:
      msg: "directory"
    when: testpath1 is directory
  - debug:
      msg: "file"
    when: testpath2 is file
  - debug:
      msg: "link"
    when: testpath3 is link
  - debug:
      msg: "link"
    when: testpath4 is link
  - debug:
      msg: "mount"
    when: testpath5 is mount

接下来我们制作以下软硬链接:

playbook 判断result ansible playbook 判断_执行状态_17


在testpath4那里跳过是因为它是硬链接,而link是判断软链接的

playbook 判断result ansible playbook 判断_playbook 判断result_18

playbook9

判断字母包含的字符串是否是纯大/小写:

判断字符串的一些tests:

lower:判断包含字母的字符串中的字母是否是纯小写,字符串中的字母全部为小写则返回真

upper:判断包含字母的字符串中的字母是否是纯大写,字符串中的字母全部为大写则返回真
vim pd9.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    str1: "abc"
    str2: "ABC"
  tasks:
  - debug:
      msg: "THis string is all lowercase"
    when: str1 is lower
  - debug:
      msg: "THis string is all uppercase"
    when: str2 is upper

playbook 判断result ansible playbook 判断_执行状态_19

playbook10

判断数值是奇数/偶数/能否被某数整除:

判断数值的一些tests

even : 判断数值是否是偶数,是偶数则返回真

odd : 判断数值是否是奇数,是奇数则返回真

divisibleby(num) : 判断是否可以整除指定的数值,如果除以指定的值以后余数为0,则返回真
vim pd10.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    num1: 5
    num2: 8
    num3: 18
  tasks:
  - debug:
      msg: "an odd number"
    when: num1 is odd
  - debug:
      msg: "an even number"
    when: num2 is even
  - debug:
      msg: "can be divided exactly by"
    when: num3 is divisibleby(3)

playbook 判断result ansible playbook 判断_vim_20

playbook11

version:可以用于对比两个版本号的大小,或者与指定的版本号进行对比,
使用语法为version('版本号', '比较操作符')当使用version吋,
支持多种风格的比较操作符,version支持的比较操作符如下

大于: >, gt

大于等于: >=, ge

小于: <,lt

小于等于: <<=,le

等于: ==, =, eq

不等于: !=, <>, ne

判断version(对比版本号):

vim pd11.yml
---
- hosts: testB
  remote_user: root
  vars:
    ver: 7.3.1213
    ver1: 7.3.1212
  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}} is greater than 7.0"
    when: ansible_distribution_version is version("7.0","gt")

playbook 判断result ansible playbook 判断_playbook 判断result_21

playbook12

判断list的子集和父集关系:

subset:判断一个list是不是另一个list的子集,是另一个list的子集时返回真

superset:判断一个list是不是另一个list的父集,是另一个list的父集时返回真
vim pd12.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    a:
    - 1
    - 4
    b: [1,2,3,4,5,6]
  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)

playbook 判断result ansible playbook 判断_字符串_22

playbook13

判断是否为字符串:

vim pd13.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testvar1: 1
    testvar2: "1"
    testvar3: a
  tasks:
  - debug:
      msg: "This variable is a string"
    when: testvar1 is string
  - debug:
      msg: "This variable is a string"
    when: testvar2 is string
  - debug:
      msg: "This variable is a string"
    when: testvar3 is string

我们发现ansible认为:数字不是字符串,但如果给数字加上信号就是字符串了。字母是字符串

playbook 判断result ansible playbook 判断_vim_23


写法2:

vim pd13.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testvar1: 1
    testvar2: "1"
    testvar3: "asfdcv"
  tasks:
  - debug:
      msg: "This variable is a string"
    when: testvar1 is string
  - debug:
      msg: "This variable is a string"
    when: testvar2 is string
  - debug:
      msg: "This variable is a string"
    when: testvar3 is string

我们发现,字母的组合也是字符串,并且加不加引号都是字符串

playbook 判断result ansible playbook 判断_vim_24

判断是否为数值:

vim pd13.yml
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    testvar1: 1
    testvar2: "1"
    testvar3: 00.10
  tasks:
  - debug:
      msg: "This variable is a number"
    when: testvar1 is number
  - debug:
      msg: "This variable is a number"
    when: testvar2 is number
  - debug:
      msg: "This variable is a number"
    when: testvar3 is number

我们发现数字加上引号后就不被ansible认定为数值了,而小数也是数值

playbook 判断result ansible playbook 判断_字符串_25