Book Image

Learning Ansible

By : Madhurranjan Mohaan, Ramesh Raithatha
Book Image

Learning Ansible

By: Madhurranjan Mohaan, Ramesh Raithatha

Overview of this book

<p>Automation includes provisioning new servers, making sure the servers adhere to their role and maintain the desired state from a configuration perspective, and orchestrating various actions across environments and deploying code as expected to all these servers. This is where Ansible steps in. It is secure, highly reliable, and minimalistic in nature. It automates configuration management, application deployment, and many other IT needs.</p> <p>Learning Ansible will equip you with the necessary skills to automate/improve your infrastructure from a configuration management perspective. You will also be able to use Ansible for one-click deployments, provisioning, and orchestrating your infrastructure.</p>
Table of Contents (16 chapters)
Learning Ansible
Credits
About the Authors
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Using Ruby modules


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...