O'Reilly logo

Ansible: Up and Running by Lorin Hochstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10. Custom Modules

Sometimes you want to perform a task that is too complex for the command or shell modules, and there is no existing module that does what you want. In that case, you might want to write your own module.

In the past, I’ve written custom modules to retrieve my public IP address when I’m behind a network address translation (NAT) getaway, and to initialize the databases in an OpenStack deployment. I’ve thought about writing a custom module for generating self-signed TLS certificates, though I’ve never gotten around to it.

Another common use for custom modules is if you want to interact with some third-party service over a REST API. For example, GitHub offers what it calls Releases, which let you attach binary assets to repositories, and these are exposed via GitHub’s API. If your deployment required you to download a binary asset attached to a private GitHub repository, this would be a good candidate for implementing inside of a custom module.

Example: Checking That We Can Reach a Remote Server

Let’s say we want to check that we can connect to a remote server on a particular port. If we can’t, we want Ansible to treat that as an error and stop running the play.

Note

The custom module we will develop in this chapter is basically a simpler version of the wait_for module.

Using the Script Module Instead of Writing Your Own

Recall in Example 6-16 how we used the script module to execute custom scripts on remote hosts. Sometimes it’s simpler to just use ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required