自动化运维工具Ansible之Tests测验详解

时间:2021-10-30 20:34:01

Ansible Tests 详解与使用案例

主机规划

自动化运维工具Ansible之Tests测验详解

添加用户账号

说明:

1、 运维人员使用的登录账号;

2、 所有的业务都放在 /app/ 下「yun用户的家目录」,避免业务数据乱放;

3、 该用户也被 ansible 使用,因为几乎所有的生产环境都是禁止 root 远程登录的(因此该 yun 用户也进行了 sudo 提权)。

 # 使用一个专门的用户,避免直接使用root用户
# 添加用户、指定家目录并指定用户密码
# sudo提权
# 让其它普通用户可以进入该目录查看信息
useradd -u -d /app yun && echo '' | /usr/bin/passwd --stdin yun
echo "yun ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
chmod /app/

Ansible 配置清单Inventory

之后文章都是如下主机配置清单

 [yun@ansi-manager ansible_info]$ pwd
/app/ansible_info
[yun@ansi-manager ansible_info]$ cat hosts_key
# 方式1、主机 + 端口 + 密钥
[manageservers]
172.16.1.180: [proxyservers]
172.16.1.18[:]: # 方式2:别名 + 主机 + 端口 + 密码
[webservers]
web01 ansible_ssh_host=172.16.1.183 ansible_ssh_port=
web02 ansible_ssh_host=172.16.1.184 ansible_ssh_port=
web03 ansible_ssh_host=172.16.1.185 ansible_ssh_port=

Tests 概述

Tests 在 Jinja 中是一种评估模板表达式,并最终返回 True 或 False。Jinja 中就有自带的 Tests 清单,具体地址如下:

http://docs.jinkan.org/docs/jinja2/templates.html#builtin-tests

tests 和 filters 的主要区别在于Jinja tests 用于比较,而 filters 用于数据操作,两者在Jinja中有不同的应用。

与所有模板一样,tests 总是在 Ansible 控制机上执行,而不是在任务的目标机上,因为它们测验本地数据。

除了 Jinja2 tests 之外,Ansible还提供了一些 tests,用户也可以轻松创建自己的 tests。

测验字符串

若要将字符串与子字符串或正则表达式匹配,请使用「match」、「search」或「regex」过滤。

match:必须有开头匹配

search:子串匹配

regex:正则匹配

示例:

 [yun@ansi-manager ansi_tests]$ pwd
/app/ansible_info/ansi_tests
[yun@ansi-manager ansi_tests]$ cat tests_str.yml
--- - hosts: manageservers
vars:
url: "http://example.com/users/foo/resources/bar" tasks:
- debug:
msg: "matched pattern 1-1"
when: url is match("http://example.com/users/.*/resources/.*") # True - debug:
msg: "matched pattern 1-2"
when: url is match("http://example.com") # True - debug:
msg: "matched pattern 1-3"
when: url is match(".*://example.com") # True - debug:
msg: "matched pattern 1-4"
when: url is match("example.com/users/.*/resources/.*") # False - debug:
msg: "matched pattern 2-1"
when: url is search("/users/.*/resources/.*") # True - debug:
msg: "matched pattern 2-2"
when: url is search("/users/") # True - debug:
msg: "matched pattern 2-3"
when: url is search("/user/") # False - debug:
msg: "matched pattern 3"
when: url is regex("example.com/\w+/foo") # True [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_str.yml # 注意查看执行

测验版本比较

使用「version」,用于版本号比较。

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

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

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

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

示例:

 # 判断 ansible_python_version 版本是否 大于等于 2.7.
[yun@ansi-manager ansi_tests]$ pwd
/app/ansible_info/ansi_tests
[yun@ansi-manager ansi_tests]$ ll
total
drwxrwxr-x yun yun Sep : file
-rw-rw-r-- yun yun Sep : tests_version.yml
[yun@ansi-manager ansi_tests]$ cat file/tests_version.conf.j2 # 涉及文件
# Jinja2 版本测验 {% if ansible_python_version is version('2.7.3', '>=') %}
result True. ansible_python_version = {{ ansible_python_version }}
{% else %}
result False
{% endif %} [yun@ansi-manager ansi_tests]$ cat tests_version.yml # 涉及的playbook文件
---
# ansible tests Version Comparison - hosts: proxyservers tasks:
- name: "Tests Version Comparison"
template:
src: ./file/tests_version.conf.j2
dest: /tmp/tests_version.conf [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_version.yml # 执行

测验子集和超集

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

示例:

 [yun@ansi-manager ansi_tests]$ pwd
/app/ansible_info/ansi_tests
[yun@ansi-manager ansi_tests]$ cat tests_set.yml
---
# tests 子集和超集
- hosts: manageservers vars:
a: [,,,,]
b: [,]
tasks:
- debug:
msg: "A includes B"
when: a is superset(b) - debug:
msg: "B is included in A"
when: b is subset(a) [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_set.yml # 注意查看执行

测验列表真假

关键字「all」和「any」,用于检查列表里元素的真假,列表中所有为真或者任何一个为真。

all:一假则假

any:一真则真

 [yun@ansi-manager ansi_tests]$ pwd
/app/ansible_info/ansi_tests
[yun@ansi-manager ansi_tests]$ cat tests_list.yml
---
# tests 测验 all any
- hosts: manageservers vars:
mylist:
-
- "{{ 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 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_list.yml # 注意查看执行

测验文件或目录

用于测验目录、文件、软连接、是否已存在、是相对路径还是绝对路径等等。

 [yun@ansi-manager ansi_tests]$ pwd
/app/ansible_info/ansi_tests
[yun@ansi-manager ansi_tests]$ cat tests_path.yml
---
- hosts: manageservers vars:
# - mypath: /tmp/
- mypath: /tmp/yum_hard.sh
- path2: /tmp/
# - path2: /tmp/yum_hard_2.sh tasks:
- 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 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_path.yml # 注意查看执行

测验任务执行结果

对任务执行结果进行测验。

 [yun@ansi-manager ansi_tests]$ pwd
/app/ansible_info/ansi_tests
[yun@ansi-manager ansi_tests]$ cat tests_result.yml
---
- hosts: 172.16.1.180 ## 对如下3种情况一次测验
tasks:
- shell: /usr/bin/foo
#- shell: /usr/bin/true
#- shell: /usr/bin/false
register: result
ignore_errors: True - debug:
msg: "{{ result }}" - 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 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_result.yml # 注意查看执行

———END———
如果觉得不错就关注下呗 (-^O^-) !

自动化运维工具Ansible之Tests测验详解