youki2008
作者youki2008·2020-06-26 10:24
系统架构师·DDT

ansible基础-Jinja2模版 | 测试

字数 3154阅读 895评论 0赞 3

[ansible基础-Jinja2模版 | 测试]

一 简介

注:本文demo使用ansible2.7稳定版

Jinja2的测试语句被用来评估一个条件表达式,并且最终返回True或False,经常和「when」语句搭配使用。

测试语句和过滤器的相同点:测试语句的条件表达式也在控制端执行,在目的主机端生效。

测试语句和过滤器的不同点:

  • 前者多被用于「比较」,执行结果是True或False,而后者多被用于对数据的操作与转换,执行结果是我们期望的数据内容或数据格式。
  • 语法不同,前者使用「is」,后者使用「|」

    测试语句的语法很简单,写法如下:

    variable is test_name

    举个,task执行结果为failed时,则返回True:

    result is failed

二 测试字符串

关键字「match」和「search」,参数可以使用正则表达式,用来查找一个字符串是否与测试语句相匹配。

vars:
  url: "http://example.com/users/foo/resources/bar"

tasks:
    - debug:
        msg: "matched pattern 1"
      when: url is match("http://example.com/users/.*/resources/.*")

    - debug:
        msg: "matched pattern 2"
      when: url is search("/users/.*/resources/.*")

    - debug:
        msg: "matched pattern 3"
      when: url is search("/users/")


通过上面示例,我们可以看出:

  • 关键字「match」用于判断一个字符串的完整匹配
  • 关键字「search」用于判断一个字符串的部分匹配

三 测试版本号

关键字「version」(旧版本为「version_compare」),用于比较版本号。

例如,测试当前centos操作系统的版本号是否大于等于「7.2.1511」,可以这样写:

{{ ansible_facts['ansible_distribution_version'] is version('7.2.1511', '>=') }}

如果当前操作系统版本号大于或等于「7.2.1511」,条件表达式返回True,否则返回False。

「version」接受的运算符如下:

<, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne

「version」也可以接受「strict」参数,这个参数默认值为「False」,如果设置为「True」则ansible会进行更严格的版本检查:

{{ sample_version_var is version('1.0', operator='lt', strict=True) }}

四 测试列表

关键字「superset」和「subset」,用于测试一个列表是否包含或被包含于另一个列表:

vars:
    a: [1,2,3,4,5]
    b: [2,3]
tasks:
    - debug:
        msg: "A includes B"
      when: a is superset(b)

    - debug:
        msg: "B is included in A"
      when: b is subset(a)


关键字「all」和「any」,用于检查列表里的元素的真假:

vars:
  mylist:
      - 1
      - "{{ 3 == 3 }}"
      - True
  myotherlist:
      - False
      - True
tasks:

  - debug:
      msg: "all are true!"
    when: mylist is all

  - debug:
      msg: "at least one is true"
    when: myotherlist is any


用大学学的离散数学概括下:

all:一假则假

any:一真则真

五 测试文件路径

测试文件路径的关键字从字面上就能看出来其含义,下面直接上示例:

- debug:
    msg: "path is a directory"
  when: mypath is directory

- debug:
    msg: "path is a file"
  when: mypath is file

- debug:
    msg: "path is a symlink"
  when: mypath is link

- debug:
    msg: "path already exists"
  when: mypath is exists

- debug:
    msg: "path is {{ (mypath is abs)|ternary('absolute','relative')}}"

- debug:
    msg: "path is the same file as path2"
  when: mypath is same_file(path2)

- debug:
    msg: "path is a mount"
  when: mypath is mount


注:这个特性在ansible>=2.5的版本中才有,在生产实践中,可以替换旧版本中「stat」+「register」+「when」实现的功能。

六 测试任务执行结果

测试任务执行结果也比较通俗易懂,示例如下:

tasks:

  - shell: /usr/bin/foo
    register: result
    ignore_errors: True

  - debug:
      msg: "it failed"
    when: result is failed

  # in most cases you'll want a handler, but if you want to do something right now, this is nice
  - debug:
      msg: "it changed"
    when: result is changed

  - debug:
      msg: "it succeeded in Ansible >= 2.1"
    when: result is succeeded

  - debug:
      msg: "it succeeded"
    when: result is success

  - debug:
      msg: "it was skipped"
    when: result is skipped


七 本节应该掌握的技能

  • 掌握测试语句与过滤器的相同点和异同点。
  • 掌握在playbook和模版中灵活运用测试语句。

八 参考链接

转载自(https://www.cnblogs.com/mauricewei/p/10056412.html)

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

3

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广