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 Python modules


Ansible intends to allow users to write modules in any language. Writing the module in Python, however, has its own advantages. You can take advantage of Ansible's libraries to shorten your code, an advantage not available for modules in other languages. Parsing user arguments, handling errors, and returning the required values becomes easier with the help of the Ansible libraries.

We will see two examples for a custom Python module, one with and one without using the Ansible library, to give you a glimpse of how custom modules work. Make sure you organize your directory structure as mentioned in the previous section before creating the module. The first example creates a module named check_user; let's look at the code in the following screenshot:

The preceding custom module, check_user, will check whether a user exists on a host. The module expects a user argument from Ansible. Let's break down the preceding module and see what it does.

We first import the libraries required...