美文网首页ansible
ansible之条件判断

ansible之条件判断

作者: 燃燃的爸爸 | 来源:发表于2019-05-05 13:24 被阅读35次

    1.判断目录或文件是否存在

    - hosts: cf601
      remote_user: root
      gather_facts: no
      vars:
        testpath: /testdir
      tasks:
      - debug:
          msg: "file exist"
        when: testpath is exists  #不存在就用testpath is not exists
    

    2.判断变量

    defined:判断变量是否已经定义,已经定义则为真
    undefined:判断变量是否已经定义,未定义则返回真
    none:判断变量是否已经定义,如果变量值已经定义,但是变量值为空,则返回真

    - hosts: cf601
      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: "The variable is defined, but there is no value"
        when: testvar1 is none
    

    3.判断执行结果

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

    - hosts: cf601
      remote_user: root
      gather_facts: no
      vars:
        doshell: "yes"
      tasks:
      - shell: "cat /testdir/abc"
        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
    

    稍加改变看看三种情况的结果

    4.判断路径

    file: 判断路径是否是一个文件,如果路径是一个文件则为真(该参数在实际使用我碰到过问题,暂时不考虑使用可以使用stat来代替例如:

    - name: check if exists /usr/local/bin/python3
      stat: path={{ py3_dir }}
      register: dir_stat
    
    - name: check py3 is installed and force exit
      fail: msg="python3 has been installed already!"
      when: dir_stat.stat.exists
    


    directory: 判断路径是否是一个目录,如果路径是一个目录则为真
    link: 判断路径是否是一个软连接,如果路径是一个软连接则为真
    mount: 判断路径是否是一个挂载点,如果路径是一个挂载点则为真
    exists: 判断路径是否存在,如果路径存在则为真
    注意:某些版本之前可能需要加上“is_”前缀

    - hosts: cf601
      remote_user: root
      gather_facts: no
      vars:
        testpath1: "/testdir/test"
        testpath2: "/testdir/"
        testpath3: "/testdir/testsoftlink"
        testpath4: "/testdir/testhardlink"
        testpath5: "/boot"
      tasks:
      - 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
      - debug:
          msg: "exists"
        when: testpath1 is exists
    

    5.判断整除(比较少用)

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

    - hosts: cf601
      remote_user: root
      gather_facts: no
      vars:
        num1: 4
        num2: 7
        num3: 64
      tasks:
      - debug:
          msg: "An even number"
        when: num1 is even
      - debug:
          msg: "An odd number"
        when: num2 is odd
      - debug:
          msg: "Can be divided exactly by"
        when: num3 is divisibleby(8)
    

    5.其他

    version: 可以用于对比两个版本号的大小,或者与指定的版本号进行对比,语法version('版本号','比较操作符')
    2.5版本此test从version_compare 更名为version

    - hosts: test70
      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.3"
        when: ansible_distribution_version is version("7.3","gt")
    

    这里的例子里面两种比较都是可以的 支持多种风格操作符。

    subset: 判断一个list是不是另一个list的子集,是则为真
    siperset: 判断一个list是不是另一个list的父集,是则为真
    注:2.5版本之前是issubset和issuperset

    - hosts: cf601
      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)
    

    string: 判断对象是否是一个字符串,是则为真
    number: 判断对象是否是一个数字,是则为真

    - hosts: cf601
      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
    - debug:
          msg: "This variable is 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
    

    转载自:http://www.zsythink.net/archives/2817

    相关文章

      网友评论

        本文标题:ansible之条件判断

        本文链接:https://www.haomeiwen.com/subject/adecoqtx.html