Provisioning WildFly 10 with Ansible

Ansible is an innovative IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

To manage a server with Ansible, the server needs to have SSH and Python 2.5 or later installed. There’s no need to preinstall an agent or any other software on the host.
Ansible works by connecting to your nodes and pushing out small programs, called "Ansible Modules" to them. These programs are written to be resource models of the desired state of the system. Ansible then executes these modules (over SSH by default), and removes them when finished.

  If you aren't familiar with Ansible, you can read this introduction HelloWorld Ansible tutorial.

This tutorial will cover a more advanced aspect, that is Ansible playbooks. Playbooks are a way to send commands to remote computers in a scripted way. Instead of using single Ansible commands, you can configure entire complex environments by passing a script to one or more systems.

Ansible playbooks are written in the YAML format which is a human readable way of a complex data structures. Each playbook contains one or more plays, which map hosts to a certain function. Ansible does this through something called tasks, which are basically module calls. The playbook we will describe will provision a vanilla implementation of WildFly Application Server version 10

1. Create a playbook

Playbooks in Ansible define a series of actions to run, and address particular sets of servers. Create a playbook.yml file:

---
- hosts: 10.3.11.47 
  remote_user: centos
  sudo: yes

  roles:
    - jboss-standalone

This playbook defines the target host and remote user to be used for provisioning. Also since we need to run a yum installation of Java, we will require running the playbook as sudoer. Most interesting, at the bottom there is a Role declaration.

Roles are ways of automatically loading certain vars_files, tasks, and handlers based on a known file structure. Grouping content by roles also allows easy sharing of roles with other users.

Please note that YAML is very sensitive to white-space, and uses that to group different pieces of information together. You should use only spaces and not tabs and you must use consistent spacing for your file to be read correctly. Items at the same level of indentation are considered sibling elements.

Within the folder roles/jboss-standalone/tasks you can find the set of tasks which are included in this Role. In our case, the following main.yml contains the installation task:

---
- name: Install Java 1.8 and some basic dependencies
  yum: name={{item}} state=present
  with_items:
   - unzip
   - java-1.8.0-openjdk
 
- name: Download WildFly from jboss.org
  get_url: url=http://download.jboss.org/wildfly/10.0.0.Final/wildfly-10.0.0.Final.zip dest=/opt/wildfly-10.0.0.Final.zip
 
- name: Extract archive
  command: chdir=/usr/share /usr/bin/unzip -q /opt/wildfly-10.0.0.Final.zip creates=/usr/share/jboss-as
 
  # Rename the dir to avoid encoding the version in the init script
- name: Rename install directory
  command: chdir=/usr/share /bin/mv wildfly-10.0.0.Final jboss-as creates=/usr/share/jboss-as
 
- name: Copying standalone.xml configuration file
  template: src=standalone.xml dest=/usr/share/jboss-as/standalone/configuration/
  notify: restart jboss
 
- name: Add group "jboss"
  group: name=jboss
 
- name: Add user "jboss"
  user: name=jboss group=jboss home=/usr/share/jboss-as
 
- name: Change ownership of WildFly installation
  file: path=/usr/share/jboss-as/ owner=jboss group=jboss state=directory recurse=yes
 
- name: Copy the init script
  copy: src=wildfly-init-redhat.sh dest=/etc/init.d/jboss mode=0755
 
- name: Enable WildFly to be started at boot
  service: name=jboss enabled=yes state=started 

 The purpose of this installation script is to:

  • Install JDK 1.8
  • Download and unzip WildFly 10
  • Create user and group "jboss" and change ownership of the installation path
  • Copy the standalone.xml configuration folder
  • Copy the wildfly-init-redhat.sh in the /etc/init.d folder
  • Enable WIldFly as a Service at boot

You can run the playbook as follows:

$ ansible-playbook -i hosts playbook.yml
PLAY [10.3.11.47] *************************************************************

GATHERING FACTS ***************************************************************
ok: [10.3.11.47]

TASK: [jboss-standalone | Install Java 1.8 and some basic dependencies] *******
changed: [10.3.11.47] => (item=unzip,java-1.8.0-openjdk)

TASK: [jboss-standalone | Download JBoss from jboss.org] **********************
changed: [10.3.11.47]

TASK: [jboss-standalone | Extract archive] ************************************
ok: [10.3.11.47]

TASK: [jboss-standalone | Rename install directory] ***************************
ok: [10.3.11.47]

TASK: [jboss-standalone | Copying standalone.xml configuration file] **********
changed: [10.3.11.47]

TASK: [jboss-standalone | Add group "jboss"] **********************************
ok: [10.3.11.47]

TASK: [jboss-standalone | Add user "jboss"] ***********************************
ok: [10.3.11.47]

TASK: [jboss-standalone | Change ownership of JBoss installation] *************
ok: [10.3.11.47]

TASK: [jboss-standalone | Copy the init script] *******************************
changed: [10.3.11.47]

TASK: [jboss-standalone | Enable JBoss to be started at boot] *****************
ok: [10.3.11.47]

NOTIFIED: [jboss-standalone | restart jboss] **********************************

We check the result on the target machine:

ansible tutorial wildfly jboss

The PlayBook is available on github at: https://github.com/fmarchioni/mastertheboss/tree/master/ansible/wildfly-standalone

Follow us on Twitter