In this article, we will be looking at the assert module, how it works, why it is needed and the different ways it can be integrated into your routine Ansible activities.
How Do Conditional Statements Work?
While coding in a regular programming language, we come across situations where we need conditional statements to solve the problem. The program needs to make some decisions to go to the next part of the problem.
An example of a conditional statement is the if and else statement. The working of the if and else statement is that if the expression after the “if” is true, the logical flow and control would go to the expression included with the “if”. However, if the condition of the “if” is false, the control goes to the expression included with “else”.
For example, look at the following pseudo-code.
print “a is 10”
else
print “a is not 10”
The compiler will first check whether “a” is 10 or not. If it is, “a is 10” will be printed, else “a is not 10” will be printed.
You can use conditional statements to direct your CPU to perform logical tasks. There are many instances where you may find if-else statements as crucial to your desired computations. The assert module in Ansible does something similar.
What is the Ansible Assert Module?
If a certain condition is true, you can “assert” that the given expression is true and print a message along with it. Assert doesn’t instruct the system to make a decision so it isn’t a conditional statement. It simply tells the system that the statement being written is true.
The assert module can be used in combination with any other module. You can integrate it with other modules so that you receive a success message as soon as the condition proves to be true. There aren’t any logical or arithmetical errors to be made or issues that could be caused when you use assert in Ansible playbooks.
Parameters
There are different parameters available with assert to make it more functional and resourceful. Given below are some parameters you’ll be using most frequently.
Fail_msg: You can use this with assert to print a statement stating that the condition mentioned with assert was not true.
Success_msg: This can be used to print a message stating that the condition mentioned with assert proved to be true.
That: This is the operator you have to use with assert to specify the condition or statement.
Quiet: Quiet can be used instead of success_msg to mention that the condition is true.
This was the basic introduction of the assert module available in Ansible. Next, let’s have a look at some examples of how assert and its different parameters are used.
Examples
This is a simple example where we are using assert in combination with “that” to state that the variable a, is 10.
that:
- a = 3
This is another way of using assert with “that” to specify the value of variable a.
that:
- example <= 10
- example >= 0
fail_msg: "'example’ must be between 0 and 10"
success_msg: "'example' is between 0 and 10"
Other than that, you can run a playbook using the following command on the Linux shell:
In this example, we are using assert, “that”, “fail_msg” and “success_msg” in the same code. As is apparent, we are specifying that the value of the “example” variable is between 0 and 10. The fail_msg prints the message saying that the value can only be in between 0 and 10 and the success_msg prints the message saying that “example” lies in the range of 0 and 10.
that:
- example <= 10
- example >= 0
msg: "'example' must be between 0 and 10"
Ansible 2.7 and earlier versions did not have the “success_msg” or “fail_msg” features. There was the simple “msg” operator that could be used to print a certain message. We have used “msg” in the above example.
that:
- my_param <= 100
- my_param >= 0
quiet: true
Lastly, in this example we have used the “quiet” operator to just specify that the written condition is true.
These were a few examples of how the assert module and its parameters are used in combination. Assert is a great module to be used in playbooks. Let’s see how that would work.
Ansible Assert in Playbooks
Playbooks are one of the primary features of Ansible. They are written in the YAML format which means “Yet Another Markup Language”. The syntax for creating playbooks is very simple and the best thing about them is playbooks are flexible thus making Ansible increasingly resourceful.
With playbooks having the assert module in them, users can create testing functions and strategies for themselves and their systems. For example, you can create a playbook that automatically checks for system updates. You can use assert and make your playbook give you an “update success” message or “updates available” message, whenever it checks for updates.
Playbooks like these are the reason why Ansible is powerful. It brings automation to our management environments which enables the users to be more efficient.
Conclusion
In this article, we looked at the assert module. As previously established, the assert module is similar to using conditional statements in regular programming languages. We looked at the function performed by assert and the way we can use different parameters available with it.
We hope you were able to grasp the whole concept of using assert and how you can use it to generate messages. For further questions, please let us know in the comments.