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:
- Share the Love: If you enjoy the articles, consider sharing them with your friends, family and social media followers. Sharing the content helps to reach a wider audience and grow a community that simply shares solving problems.
- Feedback is Appreciated: I value your feedback! Let me know what you think about the content, what topics you’d like to see more of and any suggestions you have for improving my blog. Your input helps me tailor the content to better serve you.
- Buy a Coffee: If you’d like to support financially, consider buying me a coffee. Your donation goes a long way in helping to cover the costs associated with running and maintaining the blog. Even a small contribution can make a big difference!