自动化运维工具Ansible之Tests测验详解
Ansible Tests 详解与使用案例
主机规划
添加用户账号
说明:
1、 运维人员使用的登录账号;
2、 所有的业务都放在 /app/ 下「yun用户的家目录」,避免业务数据乱放;
3、 该用户也被 ansible 使用,因为几乎所有的生产环境都是禁止 root 远程登录的(因此该 yun 用户也进行了 sudo 提权)。
1 # 使用一个专门的用户,避免直接使用root用户 2 # 添加用户、指定家目录并指定用户密码 3 # sudo提权 4 # 让其它普通用户可以进入该目录查看信息 5 useradd -u 1050 -d /app yun && echo '123456' | /usr/bin/passwd --stdin yun 6 echo "yun ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers 7 chmod 755 /app/
Ansible 配置清单Inventory
之后文章都是如下主机配置清单
1 [yun@ansi-manager ansible_info]$ pwd 2 /app/ansible_info 3 [yun@ansi-manager ansible_info]$ cat hosts_key 4 # 方式1、主机 + 端口 + 密钥 5 [manageservers] 6 172.16.1.180:22 7 8 [proxyservers] 9 172.16.1.18[1:2]:22 10 11 # 方式2:别名 + 主机 + 端口 + 密码 12 [webservers] 13 web01 ansible_ssh_host=172.16.1.183 ansible_ssh_port=22 14 web02 ansible_ssh_host=172.16.1.184 ansible_ssh_port=22 15 web03 ansible_ssh_host=172.16.1.185 ansible_ssh_port=22
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:正则匹配
示例:
1 [yun@ansi-manager ansi_tests]$ pwd 2 /app/ansible_info/ansi_tests 3 [yun@ansi-manager ansi_tests]$ cat tests_str.yml 4 --- 5 6 - hosts: manageservers 7 vars: 8 url: "http://example.com/users/foo/resources/bar" 9 10 tasks: 11 - debug: 12 msg: "matched pattern 1-1" 13 when: url is match("http://example.com/users/.*/resources/.*") # True 14 15 - debug: 16 msg: "matched pattern 1-2" 17 when: url is match("http://example.com") # True 18 19 - debug: 20 msg: "matched pattern 1-3" 21 when: url is match(".*://example.com") # True 22 23 - debug: 24 msg: "matched pattern 1-4" 25 when: url is match("example.com/users/.*/resources/.*") # False 26 27 - debug: 28 msg: "matched pattern 2-1" 29 when: url is search("/users/.*/resources/.*") # True 30 31 - debug: 32 msg: "matched pattern 2-2" 33 when: url is search("/users/") # True 34 35 - debug: 36 msg: "matched pattern 2-3" 37 when: url is search("/user/") # False 38 39 - debug: 40 msg: "matched pattern 3" 41 when: url is regex("example.com/\w+/foo") # True 42 43 [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) }}
示例:
1 # 判断 ansible_python_version 版本是否 大于等于 2.7.3 2 [yun@ansi-manager ansi_tests]$ pwd 3 /app/ansible_info/ansi_tests 4 [yun@ansi-manager ansi_tests]$ ll 5 total 8 6 drwxrwxr-x 2 yun yun 35 Sep 12 15:14 file 7 -rw-rw-r-- 1 yun yun 209 Sep 12 15:08 tests_version.yml 8 [yun@ansi-manager ansi_tests]$ cat file/tests_version.conf.j2 # 涉及文件 9 # Jinja2 版本测验 10 11 {% if ansible_python_version is version('2.7.3', '>=') %} 12 result True. ansible_python_version = {{ ansible_python_version }} 13 {% else %} 14 result False 15 {% endif %} 16 17 [yun@ansi-manager ansi_tests]$ cat tests_version.yml # 涉及的playbook文件 18 --- 19 # ansible tests Version Comparison 20 21 - hosts: proxyservers 22 23 tasks: 24 - name: "Tests Version Comparison" 25 template: 26 src: ./file/tests_version.conf.j2 27 dest: /tmp/tests_version.conf 28 29 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_version.yml # 执行
测验子集和超集
关键字「superset」和「subset」,用于测验一个列表是否包含或被包含于另一个列表
示例:
1 [yun@ansi-manager ansi_tests]$ pwd 2 /app/ansible_info/ansi_tests 3 [yun@ansi-manager ansi_tests]$ cat tests_set.yml 4 --- 5 # tests 子集和超集 6 - hosts: manageservers 7 8 vars: 9 a: [1,2,3,4,5] 10 b: [2,3] 11 tasks: 12 - debug: 13 msg: "A includes B" 14 when: a is superset(b) 15 16 - debug: 17 msg: "B is included in A" 18 when: b is subset(a) 19 20 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_set.yml # 注意查看执行
测验列表真假
关键字「all」和「any」,用于检查列表里元素的真假,列表中所有为真或者任何一个为真。
all:一假则假
any:一真则真
1 [yun@ansi-manager ansi_tests]$ pwd 2 /app/ansible_info/ansi_tests 3 [yun@ansi-manager ansi_tests]$ cat tests_list.yml 4 --- 5 # tests 测验 all any 6 - hosts: manageservers 7 8 vars: 9 mylist: 10 - 1 11 - "{{ 3 == 3 }}" 12 - True 13 myotherlist: 14 - False 15 - True 16 17 tasks: 18 - debug: 19 msg: "all are true!" 20 when: mylist is all 21 22 - debug: 23 msg: "at least one is true" 24 when: myotherlist is any 25 26 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_list.yml # 注意查看执行
测验文件或目录
用于测验目录、文件、软连接、是否已存在、是相对路径还是绝对路径等等。
1 [yun@ansi-manager ansi_tests]$ pwd 2 /app/ansible_info/ansi_tests 3 [yun@ansi-manager ansi_tests]$ cat tests_path.yml 4 --- 5 - hosts: manageservers 6 7 vars: 8 # - mypath: /tmp/ 9 - mypath: /tmp/yum_hard.sh 10 - path2: /tmp/ 11 # - path2: /tmp/yum_hard_2.sh 12 13 tasks: 14 - debug: 15 msg: "path is a directory" 16 when: mypath is directory 17 18 - debug: 19 msg: "path is a file" 20 when: mypath is file 21 22 - debug: 23 msg: "path is a symlink" 24 when: mypath is link 25 26 - debug: 27 msg: "path already exists" 28 when: mypath is exists 29 30 - debug: 31 msg: "path is {{ (mypath is abs)|ternary('absolute','relative')}}" 32 33 - debug: 34 msg: "path is the same file as path2" 35 when: mypath is same_file(path2) 36 37 - debug: 38 msg: "path is a mount" 39 when: mypath is mount 40 41 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_path.yml # 注意查看执行
测验任务执行结果
对任务执行结果进行测验。
1 [yun@ansi-manager ansi_tests]$ pwd 2 /app/ansible_info/ansi_tests 3 [yun@ansi-manager ansi_tests]$ cat tests_result.yml 4 --- 5 - hosts: 172.16.1.180 6 7 ## 对如下3种情况一次测验 8 tasks: 9 - shell: /usr/bin/foo 10 #- shell: /usr/bin/true 11 #- shell: /usr/bin/false 12 register: result 13 ignore_errors: True 14 15 - debug: 16 msg: "{{ result }}" 17 18 - debug: 19 msg: "it failed" 20 when: result is failed 21 22 # in most cases you'll want a handler, but if you want to do something right now, this is nice 23 - debug: 24 msg: "it changed" 25 when: result is changed 26 27 - debug: 28 msg: "it succeeded in Ansible >= 2.1" 29 when: result is succeeded 30 31 - debug: 32 msg: "it succeeded" 33 when: result is success 34 35 - debug: 36 msg: "it was skipped" 37 when: result is skipped 38 39 [yun@ansi-manager ansi_tests]$ ansible-playbook -b -i ../hosts_key tests_result.yml # 注意查看执行
———END———
如果觉得不错就关注下呗 (-^O^-) !