Deploying HashiCorp Nomad Using Ansible: A Step-by-Step Guide

  ·   3 min read

HashiCorp Nomad is a flexible, easy-to-use cluster manager and scheduler designed to deploy and manage applications across both on-premises and cloud environments. Ansible, a powerful IT automation tool, can be used to automate the deployment of Nomad, ensuring a consistent and repeatable process. In this article, we will walk through the steps to deploy Nomad using Ansible, focusing on creating an Ansible role for this purpose.

Prerequisites

Before we begin, ensure you have the following:

  1. Ansible: Installed on your control node. As of the latest update, Ansible 2.15 is recommended.
  2. Nomad: We will be deploying Nomad 1.6.1, the latest stable release.
  3. SSH Access: Ensure you have SSH access to the target nodes where Nomad will be installed.
  4. Python: Installed on the target nodes, as Ansible requires Python to execute tasks.

Setting Up the Ansible Role

Ansible roles are a great way to organize and reuse automation code. We will create a role named nomad to handle the installation and configuration of Nomad.

Step 1: Create the Role Directory Structure

First, create the necessary directory structure for the Ansible role:

mkdir -p ansible/roles/nomad/{tasks,files,templates,vars}

Step 2: Define Role Variables

Create a vars/main.yml file to define variables for the Nomad installation:

# ansible/roles/nomad/vars/main.yml
nomad_version: "1.6.1"
nomad_binary_url: "https://releases.hashicorp.com/nomad/{{ nomad_version }}/nomad_{{ nomad_version }}_linux_amd64.zip"
nomad_install_dir: "/usr/local/bin"
nomad_config_dir: "/etc/nomad.d"

Step 3: Create Tasks for the Role

Create a tasks/main.yml file to define the tasks for installing and configuring Nomad:

# ansible/roles/nomad/tasks/main.yml
- name: Ensure Nomad config directory exists
  file:
    path: "{{ nomad_config_dir }}"
    state: directory
    mode: '0755'

- name: Download Nomad binary
  get_url:
    url: "{{ nomad_binary_url }}"
    dest: "/tmp/nomad_{{ nomad_version }}_linux_amd64.zip"

- name: Unzip Nomad binary
  unarchive:
    src: "/tmp/nomad_{{ nomad_version }}_linux_amd64.zip"
    dest: "{{ nomad_install_dir }}"
    remote_src: yes

- name: Ensure Nomad binary is executable
  file:
    path: "{{ nomad_install_dir }}/nomad"
    mode: '0755'

- name: Create Nomad configuration file
  template:
    src: "nomad.hcl.j2"
    dest: "{{ nomad_config_dir }}/nomad.hcl"
    mode: '0644'

- name: Start Nomad service
  systemd:
    name: nomad
    enabled: yes
    state: started

Step 4: Create a Nomad Configuration Template

Create a templates/nomad.hcl.j2 file for the Nomad configuration:

# ansible/roles/nomad/templates/nomad.hcl.j2
data_dir = "/opt/nomad"

server {
  enabled = true
  bootstrap_expect = 1
}

client {
  enabled = true
}

Step 5: Create a Playbook to Use the Role

Create a playbook deploy_nomad.yml to apply the role:

# deploy_nomad.yml
- hosts: nomad_servers
  become: yes
  roles:
    - nomad

Step 6: Run the Playbook

Finally, execute the playbook to deploy Nomad:

ansible-playbook -i inventory deploy_nomad.yml

Ensure your inventory file is set up with the target nodes under the nomad_servers group.

Conclusion

By following these steps, you have automated the deployment of HashiCorp Nomad using Ansible. This approach not only saves time but also ensures consistency across environments. You can further customize the role by adding more configuration options or integrating it with other tools in your infrastructure.

References