How to Create Custom Ansible Wildfly Module – Part 2

Sharing is caring!

Introduction

After creating Wildfly Server Group in the first part, it is time to move to the second part. This post will discuss about creating server instance under each server group. Because without a server instance, server group almost has no use. So.. let’s ready for Ansible Wildfly Module part 2!

Server

Server is an actual application instance. Unlike Server Group, which is a logical group, Server needs a host for its life cycle. A host can be physical or virtual, and a physical host can contains one or more server instances.

Wildfly Server management only works on domain mode as well. Moreover, if you want to know more about Wildfly and its running mode, please take a look on its documentation here.

Use Case

Since currently there is no Ansible built-in module for Wildfly Server management, therefore I want to create a custom module for this purpose.

Here are what the module should do:

  1. Create Server instance
  2. Start Server instance
  3. Stop Server instance
  4. Remove Server instance
Cheat Sheet

This how-to is running on Python 2.7, JDK 8, Wildfly 10.0.0 and using JBoss CLI management guide from this link.

Here are commands from JBoss CLI regarding Wildfly Server management. Of course, based on the use cases :).

Step by Step

Alright, enough for cheat sheet. Too many cheating is not good, is it? Yeah, we are now ready to code Ansible Wildfly module part 2!

Step 1 – Create New Python File

Create file under library module and give it a name jcli_server.py. Then add hash bang, import statement for Ansible module utils, subprocess module for invoking shell command and time module.

Note: there is an additional import statement compare to previous post, which is time module. This module is needed in remove server function.

Step 2 – Create Function to Check Server Existence

In every use case implementation, we need to examine a Server with specific name already created or not. The reason behind is, in several cases, if a server already exists, then it will have an impact in the Ansible Wildfly module itself. Especially with the changed flag after module execution.

Note: this similar with the server group.

  1. We have six dictionary keys, which are jboss_home, server_group_name, controller_host, controller_port, user and password. These keys will be defined in the main method.
  2. Whenever the result contains WFLYCTL0216, it is assumed that the server with the given name already created.
  3. Whenever the result contains WFLYPRT0053, it is assumed that the remote host cannot be reached.
Step 3 – Create Functions for Wildfly Server Maintenance

Unlike previous post, I will merge all functions related Wildfly Server in this step. We need four functions for this purpose. Those are server_present, server_absent, server_start and server_stop. Each function will first check isServerAlreadyPresent in order to determine its next step.

server_present

server_absent

server_start

server_stop

Note:

  1. There are five additional fields keys, which are
    • host
    • server_config_name
    • server_group_name
    • server_socket_binding_port_offset
    • server_group_socket
  2. In server_absent function, we need to stop the server before removing it. Otherwise, it will return error because server is currently running. And we pause about half a second after executing stop command in order to give a chance for the server in a STOP state.
Step 4 – Define Main Function

We already collected 10 dictionary keys. Also, like always, add another one for defining Server state. So.. here is the main function looks like.

And set the main function.

Step 5 – play.yml

Now it is time to update your play.yml by adding jcli_server module.

And it is time to run your playbook.

And you will get similar result:

Ansible Wildfly Module execution
Ansible Wildfly Module execution
Wildfly Management Console
Wildfly Management Console

Conclusion

That’s all folks. Now we can configure Wildfly server via Ansible as well. In the next post I will bring how to set the JVM parameter for each server.  Hopefully not more than a week after this post :).

Author: ru rocker

I have been a professional software developer since 2004. Java, Python, NodeJS, and Go-lang are my favorite programming languages. I also have an interest in DevOps. I hold professional certifications: SCJP, SCWCD, PSM 1, AWS Solution Architect Associate, and AWS Solution Architect Professional.

Leave a Reply

Your email address will not be published. Required fields are marked *