服务器安装ansible_将Testinfra与Ansible结合使用以验证服务器状态

郑光济
2023-12-01

服务器安装ansible

通过设计, Ansible表达了计算机的期望状态,以确保将Ansible剧本或角色的内容部署到目标计算机。 但是,如果您需要确保所有基础架构更改都在Ansible中怎么办? 还是随时验证服务器的状态?

Testinfra是基础结构测试框架,可轻松编写单元测试以验证服务器状态。 它是一个Python库,并使用了功能强大的pytest测试引擎。

Testinfra入门

使用Python包管理器(pip)和Python虚拟环境可以轻松安装Testinfra。


   
   
$ python3 -m venv venv
$ source venv / bin / activate
( venv ) $ pip install testinfra

使用EPEL存储库的Fedora和CentOS软件包存储库中也提供了Testinfra。 例如,在CentOS 7上,您可以使用以下命令进行安装:


   
   
$ yum install -y epel-release
$ yum install -y python-testinfra

一个简单的测试脚本

在Testinfra中编写测试很容易。 使用您选择的代码编辑器,将以下内容添加到名为test_simple.py的文件中:


   
   
import testinfra

def test_os_release ( host ) :
    assert host. file ( "/etc/os-release" ) . contains ( "Fedora" )

def test_sshd_inactive ( host ) :
    assert host. service ( "sshd" ) . is_running is False

默认情况下,Testinfra为测试用例提供一个宿主对象。 该对象可以访问不同的帮助程序模块。 例如,第一个测试使用文件模块来验证主机上文件的内容,第二个测试用例使用服务模块来检查系统服务的状态。

要在本地计算机上运行这些测试,请执行以下命令:


   
   
( venv ) $ pytest test_simple.py
================================ test session starts ================================
platform linux -- Python 3.7.3, pytest-4.4.1, py-1.8.0, pluggy-0.9.0
rootdir: / home / cverna / Documents / Python / testinfra
plugins: testinfra-3.0.0
collected 2 items
test_simple.py ..

================================ 2 passed in 0.05 seconds ================================
文件

Testinfra和Ansible

Testinfra支持的后端之一是Ansible,这意味着Testinfra可以直接使用Ansible的清单文件和清单中定义的一组计算机对它们进行测试。

让我们以以下清单文件为例:


   
   
[ web ]
app-frontend01
app-frontend02

[ database ]
db-backend01

我们要确保我们的Apache Web服务器服务在app-frontend01app-frontend02上运行 。 让我们在名为test_web.py的文件中编写测试:


   
   
def check_httpd_service ( host ) :
    """Check that the httpd service is running on the host"""
    assert host. service ( "httpd" ) . is_running

要使用Testinfra和Ansible运行此测试,请使用以下命令:


   
   
( venv ) $ pip install ansible
( venv ) $ py.test --hosts =web --ansible-inventory =inventory --connection =ansible test_web.py

调用测试时,我们将Ansible清单[web]组用作目标计算机,并指定我们要使用Ansible作为连接后端。

使用Ansible模块

Testinfra还为Ansible提供了一个不错的API,可在测试中使用。 Ansible模块允许访问在测试中运行Ansible播放,并易于检查播放结果。


   
   
def check_ansible_play ( host ) :
    """
    Verify that a package is installed using Ansible
    package module
    """
    assert not host. ansible ( "package" , "name=httpd state=present" ) [ "changed" ]

默认情况下,Ansible的检查模式已启用,这意味着Ansible将报告如果在远程主机上执行播放会发生的变化。

Testinfra和Nagios

现在,我们可以轻松地运行测试以验证计算机的状态,我们可以使用这些测试在监视系统上触发警报。 这是捕获意外更改的好方法。

Testinfra提供与流行的监控解决方案Nagios的集成。 默认情况下,Nagios使用NRPE插件在远程主机上执行检查,但是使用Testinfra允许您直接从Nagios主服务器运行测试。

为了获得与Nagios兼容的Testinfra输出,我们必须在触发测试时使用--nagios标志。 我们还使用-qq pytest标志启用pytest的安静模式,因此不会显示所有测试详细信息。


   
   
( venv ) $ py.test --hosts =web --ansible-inventory =inventory --connection =ansible --nagios -qq line test.py
TESTINFRA OK - 1 passed, 0 failed, 0 skipped in 2.55 seconds

Testinfra是一个强大的库,用于编写测试以验证基础结构的状态。 与Ansible和Nagios结合使用,它提供了一个简单的解决方案,以代码形式实施基础架构。 它也是在使用Molecule开发Ansible角色期间添加测试的关键组成部分。


接下来要读什么

翻译自: https://opensource.com/article/19/5/using-testinfra-ansible-verify-server-state

服务器安装ansible

 类似资料: