Ansible Testing Practice

DevOps25-10-2023

Because most of my experience now revolves around development and operations (thus DevOps), I wondered how would one test the Ansible script and works as expected?

Here are some steps and techniques to help you test your Ansible scripts.

Syntax Check

Before anything else, you should always start by checking the syntax of your Ansible scripts. You can use the ansible-playbook command with the --syntax-check flag to identify any syntax errors.

ansible-playbook your_playbook.yml --syntax-check

Dry Run

Use the --check flag with ansible-playbook to perform a dry run. This checks what would be changed without actually making any changes.

ansible-playbook your_playbook.yml --check

Verbose Mode

Increase verbosity using the -v or -vv flags to get more detailed output, which can help you identify issues.

ansible-playbook your_playbook.yml -vv

Limit to a Subset

Use the --limit option to target a specific subset of hosts, which can be useful for testing on a smaller scale.

ansible-playbook your_playbook.yml --limit hostname

Use Ansible’s Debug Module

Insert debug tasks at different points in your playbook to print variable values and information for troubleshooting.

- name: Debug Information
   debug:
     var: your_variable

Use the —list-hosts Option

This option lists all the hosts that Ansible would run against. It’s useful for verifying the hosts that Ansible is targeting.

ansible-playbook your_playbook.yml --list-hosts

Check the Output

Well, after running your playbook, you can carefully review the output for any errors, warnings or unexpected behavior.

Rollback Plan

When making changes that could potentially disrupt your systems. Ensure you have a rollback plan in place which you can use if something goes wrong during the playbook execution. Kind of the same when you’re updating databases.

Run in a Testing Environment

Another good practice to first test your playbooks in a non-production environment or on a subset of your infrastructure to minimize risks. I think this is already given if you’ve been developing apps for quite some time.

Use Ansible Modules in Check Mode

Some Ansible modules support check mode. You can add the check_mode: yes parameter to tasks using such modules. This way, Ansible will only check if the desired state is achieved without making any changes.

- name: Ensure a file exists (check mode)
  ansible.builtin.file:
    path: /path/to/file
    state: touch
  check_mode: yes

Automated Testing

Consider integrating Ansible testing into your CI/CD pipeline using tools like Molecule or Ansible’s built-in ansible-test.

Manual Testing

Finally, manual testing is crucial. Validate that the desired changes have been applied correctly and that your systems are functioning as expected. But, as much as possible use this as your last option.

Final Thoughts

These are just some of the stuff I searched since I feel like I’m still “raw” when it comes to Software Engineering for DevOps. Any more you could add?

Support

Thank you for being a valued reader of my blog! Your support means the world to me and helps me continue to create valuable content for you. Here are a few ways you can show your support:

Author's photo

Herman Menor, Jr.

I'm a traveler, foodie, gamer, baller and trader viewed as a Web Developer and Software Engineer.

See other articles:

undefinedThumbnail

Solving Windows and WSL2 DNS Problem

WSL2 is a feature in Windows allowing users to run Linux directly on their Windows machines, improving performance and compatibility

wsl2, dns22-01-2024

undefinedThumbnail

Gemini: Google's version of ChatGPT

Gemini (previously called Bard) is an AI experiment by Google. You chould compare it with OpenAI's ChatGPT. They are chatbots.

ChatGPT, Bard, Goole, Gemini, OpenAI, chatbot01-09-2023