---
# System prepended metadata

title: Class Assignment 6

---

# Class Assignment 6

## Configuration Management with Ansible

In this class assignment, the objective is to create and manage the errai project developed in previous assignments and it's H2 database in different virtual machines, using Ansible for automation.

### Ansible

Ansible is an open-source automation tool, or platform, used for tasks such as configuration management, application deployment, intraservice orchestration and provisioning.

### Ansible playbook

The playbook is the core component of any Ansible configuration. An Ansible playbook contains one or multiple plays, each of which define the work to be done for a configuration on a managed server.

### Implementing Ansible
Ansible can only be installed in Unix type machines so, we will have to use a virtual machine (throught Vagrant) to  be able to execute Ansible.
We will use Vagrant to create the hosts that Ansible will manage/configure.

## Creating the hosts

The first step is to clone the example project on https://github.com/atb/vagrant-with-ansible.

After cloning the project to your repository, on the source folder, we can now create the three boxes needed (ansible box,errai application box and H2 database box).

```
vagrant up
```

This will create and launch the three boxes.

### Running H2 server

In order to simplify the task of launching the H2 database server, the vagrantfile was edited, to launch the server listening on `192.168.12:9092`

```
config.vm.define "host2" do |host2|
    host2.vm.box = "envimation/ubuntu-xenial"
    host2.vm.hostname = "host2"
    host2.vm.network "private_network", ip: "192.168.33.12"
    host2.vm.network "forwarded_port", guest: 8082, host: 8082
    host2.vm.network "forwarded_port", guest: 9092, host: 9092

    host2.vm.provision "shell", inline: <<-SHELL
      sudo apt-get install openjdk-8-jdk-headless -y
      sudo apt-get install unzip -y
      wget http://repo2.maven.org/maven2/com/h2database/h2/1.4.199/h2-1.4.199.jar
    SHELL
  
    host2.vm.provision "shell", :run => 'always', inline: <<-SHELL
      # So that the H2 server always starts
      java -cp ./h2*.jar org.h2.tools.Server -web -webAllowOthers -tcp -tcpAllowOthers -ifNotExists > ~/out.txt &
    SHELL
```

### Running the Playbook

After launching the boxes, we can now access the __Ansible__ box using `vagrant ssh ansible`.

Inside the vm, we can now run the Ansible playbook that is in the shared folder at `/vagrant/playbook1.yml`

```
ansible-playbook playbook1.yml
```

This will download Wildfly and run it in __host1__, but there are no applications for the Wildfly server to deploy, so we have to change the playbook

```yaml
    - name: Copy .war file to deployment folder
      copy: src=/vagrant/errai-demonstration-gradle-master.war dest=/opt/wildfly/standalone/deployments/errai-demonstration-gradle.war
```

With this command, the .war file of the application, can now be deployed by Wildfly.

As with a previous assignment, we need to point Wildfly to the H2 database running on __host2__ vm and remove the password from the connection to the H2 server

```yaml
    - name: Make wildfly connect to H2 database on host2
      replace:
        path: /opt/wildfly/standalone/configuration/standalone.xml
        regexp: '<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>'
        replace: '<connection-url>jdbc:h2:tcp://192.168.33.12:9092//vagrant/data/data</connection-url>'
      notify: restart wildfly
    - name: Remove password from the connection
      replace:
        path: /opt/wildfly/standalone/configuration/standalone.xml
        regexp: '<password>sa</password>'
        replace: ''
      notify: restart wildfly
```

We can now access the errai application on `http://localhost:8080/errai-demonstration-gradle` and the H2 server on `http://localhost:8082` with the connection string `jdbc:h2:tcp://192.168.33.12:9092//vagrant/data/data` so that the database in persisted even when the boxes are destroyed.

## Update Jenkins Pipeline

In order to update the Jenkins pipeline, we have to install Jenkins on the __ansible__ box.

So, to install Jenkins and Docker we have to add a few lines to the Ansible provision on the Vagrantfile

```
ansible.vm.provision "shell", inline: <<-SHELL
      sudo apt-get install -y --no-install-recommends apt-utils
      sudo apt-get install software-properties-common --yes
      sudo apt-add-repository --yes --u ppa:ansible/ansible
      sudo apt-get install ansible --yes
      sudo apt-get install openjdk-8-jdk-headless -y
      sudo wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
      sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
      sudo apt-get update -y
      sudo apt install git --yes
      sudo apt install docker.io --yes	
      sudo apt-get install jenkins --yes
    SHELL
```

After provisioning and upping the boxes again, we can now access Jenkins on `http://192.168.33.10:8080`, the Ansible box ip address.

After the initial setup, we can add the necessary credentials, create a new pipeline and direct it to the Jenkinsfile of the CA2-Part2 assignment, made on the previous assignment adding a new stage

```groovy
stage('Ansible') {
            steps {
                ansiblePlaybook become: true,
                disableHostKeyChecking: true,
                inventory: 'ca6/hosts',
                playbook: 'ca6/playbook1.yml'
            }
        }
```

There was a problem regarding memory usage on the __Ansible__ vm so a memory upgrade was made 

```groovy
ansible.vm.provider "virtualbox" do |v|
      # We need 2048Mb so that Jenkins can run
      v.memory = 2048
    end
```

## Running the pipeline

The last step of the assignment is to run the Jenkins pipeline. 

![](https://i.imgur.com/BX6CaLd.png)
