Ansible - deploy java application and Zabbix

Posted on Wed 06 January 2016 by Pavlo Khmel

This post is related to previous post "Vagrant with multiple machines and ports".

We configure application and monitoring servers. And we install Java application and Zabbix via Ansible.

1. Ansible

1.1. Install ansible

sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible

1.2. Register host in /etc/ansible/hosts

[appsrv]
appsrv ansible_ssh_port=3333 ansible_ssh_host=127.0.0.1 ansible_ssh_private_key_file=~/.vagrant.d/id_rsa
[monsrv]
monsrv ansible_ssh_port=4444 ansible_ssh_host=127.0.0.1 ansible_ssh_private_key_file=~/.vagrant.d/id_rsa

1.3. Run ansible playbook (Files below)

ansible-playbook playbook-appsrv.yml
ansible-playbook playbook-monsrv.yml
ansible-playbook playbook-deployment.yml

1.4. Reboot environment

cd ~/vagrant-homework/
vagrant reload

1.5. Files

./vagrant-homework/run_app.sh

#!/bin/bash
cd /opt
nohup /usr/bin/java HomeWork > /dev/null 2>&1 &
exit 0

./playbook-appsrv.yml

- hosts: appsrv 
  sudo: yes
  remote_user: vagrant

  tasks:
  - hostname: name=appsrv

  - service: name=iptables enabled=no
  - service: name=iptables state=stopped

  - name: Add Zabbix repository
    yum: name=http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm state=present

  - name: Install wget
    yum: name=wget state=present

  - name: Install Apache HTTP
    yum: name=httpd state=latest
    yum: name=mod_ssl state=latest

  - service: name=httpd enabled=yes

  - name: Install Zabbix-Client
    yum: name=zabbix-agent state=present

  - service: name=zabbix-agent state=started

  - service: name=zabbix-agent enabled=yes 

  - name: Configure zabbix-agent1
    command: sed -i 's/Server=127.0.0.1/Server=10.10.10.3/g' /etc/zabbix/zabbix_agentd.conf

  - name: Configure zabbix-agent2
    command: sed -i 's/ServerActive=127.0.0.1/ServerActive=10.10.10.3/g' /etc/zabbix/zabbix_agentd.conf

  - name: Configure zabbix-agent3
    command: sed -i 's/Hostname=Zabbix\ server/Hostname=appsrv/g' /etc/zabbix/zabbix_agentd.conf

  - name: Check if the userparameter_homework.conf exists.
    command: /usr/bin/test -e /etc/zabbix/zabbix_agentd.d/userparameter_homework.conf 
    ignore_errors: True
    register: zab_exists

  - name: Create userparameter_homework.conf shell
    file: path=/etc/zabbix/zabbix_agentd.d/userparameter_homework.conf state=touch owner=root group=root mode=0644
    when: zab_exists.rc != 0

  - name: Update file UserParameter=custom.homework
    lineinfile: dest=/etc/zabbix/zabbix_agentd.d/userparameter_homework.conf line="UserParameter=custom.homework,curl http://127.0.0.1:5555 2>/dev/null | awk '{ print $2 }'"  

  - name: Restart zabbix agent
    service: name=zabbix-agent state=restarted

  - name: Install SNMPD
    yum: name=net-snmp-utils state=present
    yum: name=net-snmp state=present

  - service: name=snmpd state=started

  - service: name=snmpd enabled=yes

  - name: Install OpenJDK
    yum: name=java-1.8.0-openjdk-devel state=present

  - name: Check if the app.conf exists.
    command: /usr/bin/test -e /etc/httpd/conf.d/app.conf
    ignore_errors: True
    register: app_exists

  - name: Create app.conf shell
    file: path=/etc/httpd/conf.d/app.conf state=touch owner=root group=root mode=0644
    when: app_exists.rc != 0

  - name: Make config app.conf line1
    lineinfile: dest=/etc/httpd/conf.d/app.conf line="<VirtualHost *:80>"

  - name: Make config app.conf line2
    lineinfile: dest=/etc/httpd/conf.d/app.conf line="ProxyRequests Off"

  - name: Make config app.conf line3
    lineinfile: dest=/etc/httpd/conf.d/app.conf line="ProxyPass /app http://127.0.0.1:8888"

  - name: Make config app.conf line4
    lineinfile: dest=/etc/httpd/conf.d/app.conf line="ProxyPassReverse /app http://127.0.0.1:8888"

  - name: Make config app.conf line5
    lineinfile: dest=/etc/httpd/conf.d/app.conf line="ProxyPass /mon http://127.0.0.1:5555"

  - name: Make config app.conf line6
    lineinfile: dest=/etc/httpd/conf.d/app.conf line="ProxyPassReverse /mon http://127.0.0.1:5555"

  - name: Make config app.conf line7
    lineinfile: dest=/etc/httpd/conf.d/app.conf line="</VirtualHost>"

  - name: Restart Apache HTTP
    service: name=httpd state=restarted

./playbook-monsrv.yml

---
- hosts: monsrv 
  sudo: yes
  remote_user: vagrant

  tasks:
  - hostname: name=monsrv

  - service: name=iptables enabled=no
  - service: name=iptables state=stopped

  - name: Install wget
    yum: name=wget state=present

  - name: Add Zabbix repository
    yum: name=http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm state=present

  - name: Install Zabbix-Server
    yum: name=zabbix-server-mysql state=present

  - name: Install Zabbix-Web-Mysql
    yum: name=zabbix-web-mysql state=present

  - name: Install the Python MySQL module
    yum: name=MySQL-python

  - name: Install Mysql Server
    yum: name=mysql-server state=present

  - service: name=mysqld state=started
  - service: name=mysqld enabled=yes

  - name: Create zabbix database 
    mysql_db: name=zabbix state=present collation=utf8_general_ci
    register: mysql_db

  - mysql_user: name=zabbix password=zabbix priv=*.*:ALL state=present

  - name: Import schema sql
    mysql_db: state=import name=zabbix target=/usr/share/doc/zabbix-server-mysql-2.4.6/create/schema.sql
    when: mysql_db.changed

  - name: Import images sql
    mysql_db: state=import name=zabbix target=/usr/share/doc/zabbix-server-mysql-2.4.6/create/images.sql
    when: mysql_db.changed

  - name: Import data sql
    mysql_db: state=import name=zabbix target=/usr/share/doc/zabbix-server-mysql-2.4.6/create/data.sql
    when: mysql_db.changed

  - name: Change time zone
    command: sed -i 's/# php_value date.timezone Europe\/Riga/php_value date.timezone Europe\/Kiev/g' /etc/httpd/conf.d/zabbix.conf

  - name: Change Zabbix Server conf
    command: sed -i 's/# DBHost=localhost/DBHost=localhost/g' /etc/zabbix/zabbix_server.conf

  - name: Change Zabbix Server conf 2
    command: sed -i 's/# DBPassword=/DBPassword=zabbix/g' /etc/zabbix/zabbix_server.conf 

  - name: Install SNMPD
    yum: name=net-snmp-utils state=present
    yum: name=net-snmp state=present

  - service: name=snmpd state=restarted
  - service: name=snmpd enabled=yes

  - name: Install Apache HTTP
    yum: name=httpd state=latest
    yum: name=mod_ssl state=latest

  - service: name=httpd state=restarted

  - service: name=httpd enabled=yes

  - service: name=zabbix-server enabled=yes

  - service: name=zabbix-server state=restarted

  - name: Install Zabbix-Client
    yum: name=zabbix-agent state=present

  - service: name=zabbix-agent state=started

  - service: name=zabbix-agent enabled=yes

./playbook-deployment.yml

- hosts: appsrv 
  sudo: yes
  remote_user: vagrant

  tasks:

  - name: copy app class
    shell: cp /vagrant/HomeWork.java /opt/

  - name: copy run script
    shell: cp /vagrant/run_app.sh /opt/

  - name: compile class 
    shell: cd /opt; javac HomeWork.java

  - name: autorun
    lineinfile: dest=/etc/rc.d/rc.local line="bash /opt/run_app.sh"

  - name: Start Java app
    shell: bash /opt/run_app.sh

./vagrant-homework/HomeWork.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

class NewThread extends Thread {
    protected Socket socket;

    public NewThread(Socket clientSocket) {
        this.socket = clientSocket;
    }

    public void run() {
        boolean isRunning = true;
        try
        {
            while (isRunning) {
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream());
                out.println("HTTP/1.1 200 "); 
                out.println("Content-Type: text/html"); 
                out.println("Connection: close"); 
                out.println("");
                out.println("<!DOCTYPE html>");
                out.println("<h1>Welcome... wait 5 sec...</h1>\r\n");
                String line;
                  while ((line = in.readLine()) != null) {
                        if (line.length() == 0)
                          break;
                      }
                out.flush();
                Thread.sleep(60000);
                out.close();
                HomeWork.connection_counter--;
                isRunning = false;
            }
        }
        catch(Exception e)
        {
            System.out.println("Error" + e);
        }
    }
}

class Socket0 implements Runnable
{
    public void run()
    {
        try {
            ServerSocket ss = new ServerSocket(5555);
            for (;;) {
              Socket client = ss.accept();
              BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
              PrintWriter out = new PrintWriter(client.getOutputStream());
              out.print("HTTP/1.1 200 \r\n"); 
              out.print("Content-Type: text/plain\r\n"); 
              out.print("Connection: close\r\n"); 
              out.print("\r\n");
              String line;
              out.print("Counter: " + HomeWork.connection_counter + "\r\n");
              while ((line = in.readLine()) != null) {
                if (line.length() == 0)
                  break;
              }
              out.close();
              in.close();
              client.close();
            }
          }
          catch (Exception e) {
            System.err.println(e);
          }
    }
}

public class HomeWork {
    public static int connection_counter = 0;
    public static void main(String args[]) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(8888, 0, InetAddress.getByName(null));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Service is started on local port: 127.0.0.1:8888, and monitoring port: 5555");
        Socket0 s0=new Socket0();
        Thread t1=new Thread(s0);
        t1.start();

        while (true) {
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                System.out.println("I/O error: " + e);
            }
            connection_counter++;
            new NewThread(socket).start();
            System.out.println("Connections: " + connection_counter);
        }
    }
}