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. 《深入理解 Java 虚拟机》读书笔记:垃圾收集器与内存分配策略

    正文 垃圾收集器关注的是 Java 堆和方法区,因为这部分内存的分配和回收是动态的.只有在程序处于运行期间时才能知道会创建哪些对象,也才能知道需要多少内存. 虚拟机栈和本地方法栈则不需要过多考虑回收的 ...

  2. 13. 罗马数字转整数----LeetCode

    13. 罗马数字转整数 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 ...

  3. SpringBoot集成MyBatis底层原理及简易实现

    MyBatis是可以说是目前最主流的Spring持久层框架了,本文主要探讨SpringBoot集成MyBatis的底层原理.完整代码可移步Github. 如何使用MyBatis 一般情况下,我们在Sp ...

  4. 3. git获取历史版本

    1.使用gitbash进入git命令行,查看commit记录.操作如下: git log 1 2.找到你想提取的目标版本,复制对应的SHA值. 3.新建一个分支,操作如下: git branch 新分 ...

  5. 4. css事件

    可通过使用css伪类实现点击元素变色的效果,两个伪类是:active, :focus :active :active选择器用于选择活动链接.当在一个链接上点击时,它就会成为活动的(激活的),:acti ...

  6. Springboot:员工管理之国际化(十(3))

    1:IDEA编码设置UTF-8 2:创建国际化文件 i18n\login.properties #默认语言 i18n\login_en_US.properties #英文语言 i18n\login_z ...

  7. IOC 概念

    转摘:https://www.cnblogs.com/DebugLZQ/archive/2013/06/05/3107957.html 博文目录 1.IOC的理论背景 2.什么是IOC 3.IOC也叫 ...

  8. <cstring>中常用的两个函数memset()和memcpy()

    <cstring>是c++对c中的<string.h>进行了重写,这两个头文件中的函数用法是一样的,所以在用的时候包含哪个头文件都行.下面介绍一下 <cstring> ...

  9. java学习(第五篇)包装类

    一.Integer package com.test01; public class IntegerTest01 { public static void main(String[] args) { ...

  10. 透彻理解C++11新特性:右值引用、std::move、std::forward

    目录 浅拷贝.深拷贝 左值.右值 右值引用类型 强转右值 std::move 重新审视右值引用 右值引用类型和右值的关系 函数参数传递 函数返还值传递 万能引用 引用折叠 完美转发 std::forw ...