Ansible: Playbook and Role testing

preview_player
Показать описание

Ansible testing using Assert
When we run Ansible to manage server configurations, we assume (if there is no red errors) that it worked. But if you are developing ansible modules for your systems, and want to take the DevOps approach and work on CICD pipelines, you need to have some tests in there to prove that what you asked ansible to do has actually worked.

One of the best ways to do this within ansible, is to use the Assert module. It asserts that a given expression is true. When you combine this with the output of a command that’s registered to a variable, there is almost no limit to what you can test.

I’m going to use the TDD (Test driven development) method where we create the tests before we start writing any ansible code to manage our systems. I expect the tests to fail. Then we’ll write ansible to pass the tests. That’s it.

This demo will cover the following:

• Create some tests using the command and assert modules.
• Run them and confirm they are failing.
• Write Ansible code to pass the tests.
• Re-run the tests and confirm they pass.

Applications for this include CICD pipelines but remember, If you wite new code to do something, and then don’t write a test for it, you’ll never know it failed. So always think of a way to prove your code works.

I.E. if you’re installing a DB, don’t write a test to check the package is installed or the configuration file contains the right settings, just write one test to check you can connect to the DB. If everything else worked, the test should pass.

Links:

If you like the demo, hit subscribe for more videos like this and give it a like.
Рекомендации по теме
Комментарии
Автор

Is a way to print only the msg statement in assert module. Instead of all output? I see a lot of output mainly with with-items with assert module

sravanikishoresravs
Автор

Hello Dennis,
Such a great video and well elaborated!
Do you happen to do a little networking as well, cause I'd love to see how I could write some test to verify my network configuration.
I am a little new to Ansible, but I get general idea and practice. 🙂

Fireflierification
Автор

Thx for this video.
Do you have input to go further ? I mean
- run the playbook on a container before testing it
- display the test results in a human readable format (html report or anything else)

oli