Using Ansible Modules with Testinfra
I’ve been looking at improving the quality of the testing I do with Molecule and Testinfra. Simple checks like service.is_running or package.is_installed have their place but they’re pretty limited as to what assurances they provide us. Part of the issue I have is that some tests need a fair bit of setup to make them worthwhile. Attempting to tackle that with raw Python might be a little bit tricky. A better approach is to use the Ansible module available within TestInfra. We can call an ansible module with the following Python code:
Some of the stuff I do involves Nagios Plugins that parse log files for interesting content. I can test the the plugin is doing what it’s supposed to by setting log file content and then running the plugin and checking the output. Here’s an example of that…
The dummy_log_content.log is placed into the roles molecule/default/files directory. By providing a range of dummy log files I can ensure the Nagios Plugin is working as expected.