Ansible: how to test a playbook?
- User
Ansible: how to test a playbook?
Assistant
Testing an Ansible playbook is essential to ensure it works as expected before deploying it to production. Here are several approaches to test an Ansible playbook:
1. Syntax Check (
ansible-playbook --syntax-check)Before running a playbook, check for syntax errors:
ansible-playbook playbook.yml --syntax-check
2. Dry Run (
--checkor--diff)Run the playbook in check mode to see what changes would be made without actually applying them:
ansible-playbook playbook.yml --checkTo see differences (
--diff), use:ansible-playbook playbook.yml --check --diff
3. Run on a Limited Set of Hosts (
--limit)Test the playbook on a subset of hosts (e.g., a test server):
ansible-playbook playbook.yml --limit "test_host"
4. Use
ansible-lintfor Best PracticesInstall
ansible-lintto check for common issues:pip install ansible-lint ansible-lint playbook.yml
5. Test with
molecule(Advanced Testing)Molecule is a framework for testing Ansible roles and playbooks. It allows you to:
- Test in isolated environments (Docker, Vagrant, etc.).
- Verify idempotency (running the playbook multiple times should not change the system after the first run).
- Use test cases with
testinfraorpytest.
Example:
pip install molecule molecule-docker molecule test
6. Idempotency Test
Run the playbook twice and ensure no changes are made the second time:
ansible-playbook playbook.yml ansible-playbook playbook.yml | grep -q 'changed=0.*failed=0' && echo "Idempotency test passed" || echo "Idempotency test failed"
7. Use
ansible --list-hoststo Verify InventoryCheck which hosts the playbook will target:
ansible-playbook playbook.yml --list-hosts
8. Debugging with
-v(Verbose Mode)Run the playbook with verbose output to troubleshoot:
ansible-playbook playbook.yml -v
9. Test with
ansible --versionand Environment ChecksEnsure Ansible and dependencies are correctly installed:
ansible --version
10. Use
assertModule for ValidationAdd assertions in your playbook to verify conditions:
- name: Verify a condition ansible.builtin.assert: that: - "'success' in some_variable" fail_msg: "The condition was not met!"
Best Practices for Testing
- Test in a staging environment before production.
- Use version control (Git) to track changes.
- Automate testing in CI/CD pipelines (e.g., GitHub Actions, Jenkins).
- Test individual roles before combining them in a playbook.
By following these methods, you can ensure your Ansible playbooks are reliable and safe to deploy. 🚀
- User
What purpose has the variable ansible_check_mode?
Assistant
In Ansible, the
ansible_check_modevariable is a special boolean variable that indicates whether the playbook is running in check mode (also known as "dry-run" mode).Purpose of
ansible_check_modeWhen you run an Ansible playbook with
--check(or-C), Ansible simulates the changes that would be made without actually applying them. Theansible_check_modevariable is set totruein this case, allowing tasks to behave differently in check mode.How It Works
- If
ansible_check_modeistrue, tasks should report what would change but not make any modifications. - If
ansible_check_modeisfalse(default), tasks execute normally.
Example Usage
Some modules (like
command,shell, or custom modules) may need to handle check mode explicitly. For example:- name: Run a command only if not in check mode ansible.builtin.command: "/usr/bin/some_script.sh" when: not ansible_check_modeCommon Use Cases
-
Conditional Execution
- Skip tasks that shouldn’t run in check mode.
- Example: Avoid running destructive commands in dry-run.
-
Module-Specific Behavior
- Some modules (like
templateorcopy) automatically handle check mode by comparing files and reporting changes. - Others (like
commandorshell) may need manual handling.
- Some modules (like
-
Custom Module Development
- If you write a custom Ansible module, you can check
ansible_check_modeto modify behavior.
- If you write a custom Ansible module, you can check
How to Check Its Value
You can debug its value in a playbook:
- name: Debug check mode status ansible.builtin.debug: var: ansible_check_modeRunning in Check Mode
To enable check mode:
ansible-playbook playbook.yml --checkor
ansible-playbook playbook.yml -CConclusion
ansible_check_modeis useful for: ✅ Testing playbooks safely before applying changes. ✅ Conditionally modifying task behavior in dry-run mode. ✅ Ensuring idempotency by verifying what would change.This variable helps make Ansible playbooks more robust and predictable. 🚀
- If
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.