AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

ansible.builtin.assert Module: Validate Variables & Conditions (Guide)

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

How to use Ansible assert module (ansible.builtin.assert) to validate variables, check conditions, and fail gracefully. Practical YAML examples with that, fail_msg, success_msg parameters.

Introduction

The assert module in Ansible is a powerful tool for validating conditions and ensuring the correctness of your automation workflows. By halting execution when a condition isn't met, it prevents cascading failures and provides valuable feedback to the user. In this article, we'll explore the quiet, fail_msg, and success_msg parameters, which enhance the flexibility and usability of assertions in playbooks.

What is the assert Module?

The assert module evaluates one or more conditions and ensures they are true. If any condition fails, the module raises an error and stops execution. It's particularly useful for debugging, validation, and ensuring preconditions in complex playbooks.

Basic Syntax

If some_variable doesn't equal expected_value, the playbook will fail at this step.

Enhancing Assertions with quiet, fail_msg, and success_msg

1. quiet: Suppress Output for Passed Assertions

By default, the assert module outputs all evaluated conditions. However, in scenarios where you only want feedback on failures, quiet: true suppresses success messages.

Example:

Use Case: • Keeps playbook output clean and focused, especially when numerous assertions are involved.

---

2. fail_msg: Custom Error Messages for Failures

The fail_msg parameter allows you to define a custom error message displayed when the assertion fails. This is invaluable for providing context or instructions for resolving issues.

Example:

Output on Failure:

Use Case: • Improves debugging by clarifying the cause of failure and suggesting corrective actions.

---

3. success_msg: Provide Feedback on Success

The success_msg parameter lets you display a custom message when the assertion passes. This is useful for confirming critical preconditions or providing reassurance to the user.

Example:

Output on Success:

Use Case: • Provides positive feedback during playbook execution, boosting confidence in critical tasks.

---

Combining All Parameters: A Comprehensive Example

Here's how to use quiet, fail_msg, and success_msg together in a real-world scenario:

Output on Success:

Output on Failure:

---

Conclusion

The assert module is an essential tool for creating robust and reliable playbooks. By leveraging quiet, fail_msg, and success_msg, you can tailor its behavior to provide clear, actionable feedback, keeping your automation workflows efficient and user-friendly. Incorporate these features into your playbooks to streamline validation and debugging.

For more tips and real-world examples, explore my book Ansible by Example or check out additional resources below.

Related ArticlesAnsible builtin command Module: Complete Guide with Examples and Best Practicesansible.builtin.set_fact Module: Set Variables Dynamically (Complete Guide)

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home