The automation and deployment tool Ansible is able to configure applications in multiple environments. This is possible due to variables used in Roles.
Variables are key to make roles reusable, but without proper documentation or descriptions they can get complicated.
If you want to provide different scenarios which are defined by specific variables, you can provide a good documentation and hope it will be seen and understood, otherwise users will get failed runs.
But instead of failed runs with cryptic messages which show that the used variables are wrong, we can easily check beforehand if the variables are set correctly.
The Ansible module ansible.builtin.assert can provide the solution to this problem.
With Ansible when expressions you can define rules and check variables. If the conditions aren’t true the module will fail with a custom message or if true, reward the user with a custom “OK” message.
- assert: that: role_enable_feature is true and role_feature_settings is defined success_msg: The feature is configured correctly and will be managed. fail_msg: To use the feature please configure the variable role_feature_settings, please look at the documentation section X.
With this module it’s easy to guide users when they forgot to use a specific variable or if they use multiple variables which shouldn’t be used together.
If you want to learn more about Ansible, checkout our Ansible Trainings or read more on our blog.