Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Testinfra and Ansible to verify server status

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use Testinfra and Ansible to verify server status". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Testinfra is a powerful library that can be used to write tests to verify the state of the infrastructure. In addition, it combines with Ansible and Nagios to provide a simple solution for architecture as code (IaC).

By design, Ansible communicates the desired state of the machine to ensure that the content of the Ansible script or character is deployed to the target machine. But what if you need to make sure that all infrastructure changes are in Ansible? Or do you want to verify the status of the server at any time?

Testinfra is an infrastructure testing framework that makes it easy to write unit tests to verify the state of the server. It is a Python library that uses the powerful pytest test engine.

Start using Testinfra

You can easily install Testinfra using the Python package Manager (pip) and the Python virtual environment.

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

Testinfra can also be used in EPEL repositories of Fedora and CentOS. For example, on CentOS 7, you can install it using the following command:

$yum install-y epel-release$ yum install-y python-testinfra A simple test script

Writing tests in Testinfra is easy. Using the code editor of your choice, add the following to the file named 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

By default, Testinfra provides a host object for test cases that can access different helper modules. For example, * tests use the file module to verify the contents of files on the host, and the second test case uses the service module to check the status of the systemd service.

To run these tests locally, execute the following command:

(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.0rootdir: / home/cverna/Documents/Python/testinfraplugins: testinfra-3.0.0collected 2 itemstest_simple.py. = = 2 passed in 0.05 seconds = =

For a complete list of Testinfra API, you can refer to the documentation.

Testinfra and Ansible

One of the backends supported by Testinfra is Ansible, which means that Testinfra can test them directly using Ansible's manifest file and a set of machines defined in the manifest.

Let's use the following manifest file as an example:

[web] app-frontend01app-frontend02 [database] db-backend01

We want to make sure that our Apache Web server is running on app-frontend01 and app-frontend02. Let's write the test in a file called test_web.py:

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

To run this test using Testinfra and Ansible, use the following command:

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

When invoking the test, we use the [web] group of the Ansible manifest file as the target computer and specify that we want to use Ansible as the connection backend.

Use the Ansible module

Testinfra also provides a good API for Ansible to test. The Ansible module can run the Ansible action in the test and can easily check the status of the action.

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"]

By default, the inspection mode for Ansible is enabled, which means that Ansible reports changes that occur when the action is performed on the remote host.

Testinfra and Nagios

Now we can easily run tests to verify the state of the machine, and we can use these tests to trigger alarms on the monitoring system. This is a good way to capture unexpected changes.

Testinfra provides integration with Nagios, which is a popular monitoring solution. By default, Nagios uses the NRPE plug-in to check remote hosts, but Testinfra allows you to run tests directly from the Nagios host node.

To make the Testinfra output compatible with Nagios, we must use the-- nagios flag when triggering the test. We also use the pytest flag-qq to enable the silent mode of pytest so that all the test details are not displayed.

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

Testinfra is a powerful library that can be used to write tests to verify the state of the infrastructure. In addition, in combination with Ansible and Nagios, a simple solution for architecture as code (IaC) is provided. It is also a key component of adding tests to the development of Ansible roles using Molecule.

That's all for "how to use Testinfra and Ansible to verify server status". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

  • How to manually add JAR packages downloaded to the local Maven repository

    This article mainly introduces how to download to the local JAR package to manually add to the Maven warehouse, the article is very detailed, has a certain reference value, interested friends must read it!

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report