Writing modules in Ruby is as easy as writing a module in Python or bash. You just need to take care of the arguments, errors, return statements, and of course, know basic Ruby! Let's see what a Ruby module looks like in the following screenshot:
In the preceding module, we first process the user arguments, then copy the file using the rsync
library, and finally, return the output. Let's break down the preceding code and see how it works.
We first wrote a method, print_message
, which will print the output in a JSON format. By doing this, we can reuse the same code in multiple places. Remember, the output of your module should contain failed => true
if you want the Ansible run to fail; otherwise, Ansible will think that the module succeeded and will continue with the next task. The output obtained is as follows:
We then process the arguments
file, which contains a key-value pair separated by a whitespace character. This is similar to what we did with the Python module earlier...