Skip to content

Instantly share code, notes, and snippets.

@dmccuk
Last active July 5, 2023 07:49
Show Gist options
  • Save dmccuk/9e398602ede6690f04d5663734d09808 to your computer and use it in GitHub Desktop.
Save dmccuk/9e398602ede6690f04d5663734d09808 to your computer and use it in GitHub Desktop.
Prove ansible did what is said it did!
-- 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:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/assert_module.html
https://en.wikipedia.org/wiki/Test-driven_development
If you like the demo, hit subscribe for more videos like this and give it a like.
-- The test playbook:
---
- name: tests
hosts: all
become: yes
gather_facts: no
tasks:
- name: store date output for timezone check
command: date
register: check_tz
- name: check tz
assert:
that: "'UTC' in check_tz.stdout"
- name: "Check if NGINX is installed"
package_facts:
manager: "auto"
- name: confirm nginx is installed
assert:
that: "'nginx' in ansible_facts.packages"
- name: Check if port 80 is listening
shell: lsof -i -P -n | grep LISTEN
register: port_check
- name: confirm port 80 is listening
assert:
that: "'*:80 (LISTEN)' in port_check.stdout"
-- The FIX playbook:
---
- name: tests
hosts: all
become: yes
gather_facts: no
tasks:
- name: set timezone to CEST
timezone:
name: CET
- name: install nginx
yum:
name: nginx
state: installed
- name: start nginx
service:
name: nginx
state: started
-- Commands:
ansible-playbook tests.yml
ansible-playbook fix.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment