Skip to Main Content

WSO2 API Manager with Ansible

This article is on writing a simple Ansible playbook to download the WSO2 APIM product and change the port to run the server.

What is Ansible?

Prior to getting started with the playbook, I will take you around Ansible. Ansible is an automation tool widely used in the current industry. Developer tasks such as deploying, monitoring and configuring applications are made easy. If you are new to Ansible, I would recommend having a look here starting-with-ansible and checking the following article to learn how to write a simple Ansible playbook writing-simple-ansible-playbook.

How to install WSO2 API Manager using Ansible

So, let’s get started with the WSO2 API Manager and Ansible Playbook. In this playbook, we are going to;

  • Step 1: Download the APIM pack and then extract the product. 
  • Step 2: Create a role as “carbon”. 
  • Step 3: Change the offset using a jinja2 template 
  • Step 4: Run the product server.

Getting Started: Paste the following code snippet to the site.yml file.

- hosts: localhost

roles:

- carbon

Now, we need to generate folders related to the user. For that, execute the following command:

ansible-galaxy init carbon

You can see the following folder structure.

Open the main.yml file in /tasks directory and add the following commands.

# tasks file for carbon
- name: Create WSO2 APIM folder
command: mkdir wso2_apim
- name: Download WSO2 API Manager Pack
command: wget --user --ask-password https://product-dist.wso2.com/products/api-manager/2.6.0/wso2am-2.6.0.zip
- name: Move wso2 pack to WSO2 APIM folder
command: mv wso2am-2.6.0.zip ~//wso2_apim
- name: Redirect to WSO2 APIM folder
command: cd wso2_apim
- name: Extract WSO2 Pack
command: unzip -q ~//wso2_apim/wso2am-2.6.0.zip -d ~//wso2_apim

Next, we need to create a template with jinja2 to change the offset. For this task, we have to create a carbon.xml.j2 file in the /roles/carbon/templates directory. (I got the file from a downloaded APIM product and changed the extension). Change the offset as <Offset>{{ offset }}</Offset>. {{ offset }} is a variable name which we are going to change. You can use any valid variable name you want.

Next, we need to define the variable we declared in our template. This is done in the main.yml in the directory /roles/carbon/vars. Paste the following code snippet in the main.yml.

# vars file for carbon
offset: "2"

The final touch is to add the new commands to the tasks. Open the file again /roles/carbon/tasks/main.yml and paste the following code snippet:

- name: Overwrite file
template:
src: ~//roles/carbon/templates/carbon.xml.j2
dest: ~//wso2_apim/wso2am-2.6.0/repository/conf/carbon.xml
- name: Starting WSO2 API Manager....
command: sh ~//wso2_apim/wso2am-2.6.0/bin/wso2server.sh

Now we are all set with our playbook and ready to move on. Execute the following command from the project directory. (Make sure you have provided your email in the tasks, before executing the playbook)

ansible-playbook site.yml

You will get the following output;

You can confirm the port offset by checking the logs (Open a new tab and execute tail -f /<APIM_HOME>/repository/logs/wso2carbon.log) To shutdown the server press crtl+c in the playbook.

In a nutshell, we wrote a playbook to download the WSO2 API Manager, created a template with jinja2 to change the offset and declared the variable in vars. Finally, we executed the playbook.