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
. To do so, we will need to create the check_user
file in the library
folder within the Ansible top-level directory, with the following content:
#!/usr/bin/env python import pwd import sys import shlex import json def main(): # Parsing...