Using Ansible to Automate Application Deployment

PUBLISHED ON NOV 16, 2016

Using Ansible to Automate Deployment of a Go Application

Using Ansible to deploy a Go programming language application using Systemd. In order to automate deployment of a potentially complex and distributed system Ansible provides a powerful solution.

This guide focuses on the steps used to deploy a distributed web application. This web app allows the user to check ICMP availability from multiple locations. This application hosts multiple servers running the ICMP tests and a frontend server running the web server for the UI.

In order to automate the deployment of this cluster Ansible and Systemd were used to make distribution of the application and pulling in any new changes easy.

Creating Systemd Unit File

Create a systemd unit file to control your server processes.

[Unit]
Description=pingo-server monitoring daemon

[Service]
Type=simple
ExecStart=/opt/pingo/server/server
User=root

[Install]
WantedBy=multi-user.target

Creating Hosts List

Create a file called hosts containing your application servers grouped by function and environment.

[pingo-servers]
fr1.example.com
tor1.example.com
lon1.example.com
sgp1.example.com

[pingo-ui]
tor1.example.com

Creating Ansible Playbook .yml File

Create a file called deploy.yml and add your servers configuration here. Divide the playbook files into divisions by server function.

---

- name: Install pingo-server
  hosts: pingo-servers
  remote_user: root

  roles:
    - pingo-server

- name: Install pingo-ui
  hosts: pingo-ui
  remote_user: root

  roles:
    - pingo-ui

Create roles for the server deployment

Create the ansible playbook for each server role. See the documentation for how to structure your roles directory. The following example would be placed in the pingo-server/task/main.yml file.

---

- name: Install git
  apt: name=git state=installed update_cache=yes

- name: Copy pingo git repo
  git: repo=https://github.com/MajorMJR/pingo.git dest=/opt/pingo

- name: Copy pingo.service systemd unit file
  template: src=pingo-server.service.j2 dest=/etc/systemd/system/pingo-server.service owner=root mode=0644

- name: Copy pingo configuration file
  template: src=conf.json.j2 dest=/opt/pingo/server/conf.json owner=root mode=0644

- name: Enable pingo-server as system daemon
  service: name=pingo-server.service enabled=yes state=restarted
  notify: reload systemd

Run Ansible Playbook

Start the Ansible playbook with the ansible-playbook command, be sure the specify your hosts file.

ansible-playbook -i hosts deploy.yml