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测验详解的更多相关文章

  1. 自动化运维工具Ansible之Roles测验详解

    Ansible Roles 详解与实战案例 主机规划 添加用户账号 说明: 1. 运维人员使用的登录账号: 2. 所有的业务都放在 /app/ 下「yun用户的家目录」,避免业务数据乱放: 3. 该用 ...

  2. 自动化运维工具Ansible的部署步骤详解

    本文来源于http://sofar.blog.51cto.com/353572/1579894,主要是看到这样一篇好文章,想留下来供各位同僚一起分享. 一.基础介绍 ================= ...

  3. 自动化运维工具-Ansible之7-roles

    自动化运维工具-Ansible之7-roles 目录 自动化运维工具-Ansible之7-roles Ansible Roles基本概述 Ansible Roles目录结构 Ansible Roles ...

  4. 自动化运维工具Ansible详细部署 (转载)

    自动化运维工具Ansible详细部署 标签:ansible 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://sofar.blog. ...

  5. 自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客

    自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客 自动化运维工具Ansible详细部署

  6. CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机

    使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...

  7. 自动化运维工具Ansible介绍

    一个由 Python 编写的强大的配置管理解决方案.尽管市面上已经有很多可供选择的配置管理解决方案,但他们各有优劣,而 ansible 的特点就在于它的简洁. 让 ansible 在主流的配置管理系统 ...

  8. 在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操

    前言 Ansible是一款优秀的自动化IT运维工具,具有远程安装.远程部署应用.远程管理能力,支持Windows.Linux.Unix.macOS和大型机等多种操作系统. 下面就以CentOS 7.6 ...

  9. 自动化运维工具-Ansible基础

    目录 自动化运维工具-Ansible基础 什么是Ansible 同类型软件对比 Ansible的功能及优点 Ansible的架构 Ansible的执行流程 安装Ansible ansible配置文件 ...

随机推荐

  1. Erlang语言之简述及安装

    1. 简述 Erlang在1991年由爱立信公司向用户推出了第一个版本,经过不断的改进完善和发展,在1996年爱立信又为所有的Erlang用户提供了一个非常实用且稳定的OTP软件库并在1998年发布了 ...

  2. Java 理论与实践: 流行的原子

    Java 理论与实践: 流行的原子 新原子类是 java.util.concurrent 的隐藏精华 在 JDK 5.0 之前,如果不使用本机代码,就不能用 Java 语言编写无等待.无锁定的算法.在 ...

  3. android学习笔记——利用BaseAdapter生成40个列表项

    RT: main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  4. 全网最全最细的fiddler使用教程以及工作原理

    目录:导读 一.Fiddler抓包工具简介 二.Fiddler工作原理 三.Fiddler安装 四.Fiddler界面介绍​ 五.Fiddler菜单栏介绍 六.Fiddler工具栏介绍 七.Fiddl ...

  5. Django文档阅读-Day2

    Django文档阅读 - Day2 Writing your first Django app, part 1 You can tell Django is installed and which v ...

  6. fasttext 和pysparnn的安装

  7. 通用Mapper使用

    通用Mapper介绍 产生背景 使用Mybatis的开发者大多会因为繁多的XML映射配置而头痛不已

  8. 2019-2020-1 20199303《Linux内核原理与分析》第八周作业

    Linux如何启动并装载程序 理解编译链接的过程和ELF可执行文件格式 第一步:先编辑一个hello.c 第二步:生成预处理文件hello.cpp gcc -E -o hello.cpp hello. ...

  9. 定期清理nohup.out

    事件背景 服务应用weblogic通过nohup启动. nohup的使用全部都在weblogic域中的bin目录下 但是没有做定期nohup.out的清理 导致核心服务的日志过大,在出现问题时候难以打 ...

  10. Libra教程之:Libra protocol的逻辑数据模型

    文章目录 Libra protocol简介 逻辑数据模型 账本状态 交易 账本历史 Libra protocol简介 Libra区块链本质上是一个加密数据库,这个数据库是通过Libra protoco ...