An Ansible playbook for deploying Nomad + Consul on an Ubuntu cluster.
Nomad is a workload orchestrator by HashiCorp. It schedules and runs containerised and bare-metal applications across a cluster of machines, similar in spirit to Kubernetes but simpler to operate.
Consul is a service mesh and service discovery tool, also by HashiCorp. It provides a distributed key-value store, health checking, and DNS-based service discovery. Nomad integrates with Consul natively to handle cluster membership and service registration.
This playbook (like its Nomadintosh counterpart and the Nomadable parent that composes them) provisions the Nomad + Consul agents themselves — it does not deploy the job specs those agents run. Job specs live in dedicated repos: Jellify/Nomad-Jobs (Terraform-managed) and a legacy hand-deployed nomad-jobs repo.
- Ansible installed on the control machine
- Target hosts running Ubuntu (22.04 LTS or later)
- SSH access to all hosts in the inventory
Each host's Nomad datacenter is its DNS domain label (euler.jellify.app → jellify), and the Consul datacenter is the Consul servers' domain label — so hosts must be listed by fully qualified name. Inventory groups are freeform: use them for roles and Nomad job targeting (every client publishes them as meta.inventory_groups), mixing hosts across datacenters.
Host variables:
| Variable | Values | Purpose |
|---|---|---|
server.enabled |
true / (absent) |
Configures the host as a Nomad/Consul server |
docker.enabled |
true / (absent) |
Installs Docker Engine and enables the Nomad docker plugin |
nas_host |
NAS address | Required whenever nfs_mounts_shares is set — the NFS server to mount from. No default; set it in your inventory or extra vars |
nfs_mounts_shares |
list of {share_export_path} |
NFS shares to mount from nas_host. Mount point is always volume_mount_path (/mnt) + /<name>, <name> being share_export_path's final path component, lowercased |
volumes |
list of {name, path} |
Nomad host volumes to declare in client { }, typically pointed at an nfs_mounts_shares mount point |
Example host definition:
node1.example.com:
server:
enabled: true
node2.example.com:
docker:
enabled: true
nfs_mounts_shares:
- share_export_path: /var/nfs/shared/Shared # mounts at /mnt/shared
- share_export_path: /var/nfs/shared/Jellify # mounts at /mnt/jellify
volumes:
- name: Shared
path: /mnt/shared
- name: Jellify
path: /mnt/jellify
node3.example.com: {}Nomad's datacenter is always this host's DNS domain label and is purely a job-placement tag. Consul's datacenter is the domain label shared by the inventory's server.enabled: true hosts (falling back to this host's own label when the inventory has no servers) — unless you set existing_consul_datacenter and existing_cluster_servers (e.g. via Semaphore variable groups or extra-vars), in which case these hosts instead join an already-running external control plane (such as one managed by a separate Ansible project) under that fixed datacenter name.
Ansible Galaxy has no synopsis/description field for playbooks shipped inside a collection (unlike roles, which get one from meta/main.yml), so this is the canonical place it's documented:
| Playbook | Description |
|---|---|
playbooks/deploy.yml |
Full deployment — installs and configures Consul, Nomad, Docker, and NFS mounts according to each host's inventory variables. See What it does below for the full breakdown. Idempotent — safe to rerun. |
playbooks/reboot.yml |
Reboots every host in the inventory one at a time (serial: 1) via Ansible's reboot module, waiting up to 5 minutes for each to come back before moving to the next. Does not run the full deployment; pair it with playbooks/deploy.yml if a deploy is also needed. |
playbooks/clean.yml |
Runs apt autoremove to prune packages left behind by upgrades that nothing else depends on anymore — see the clean role's README. Does not run the full deployment. |
Run a full deployment:
ansible-playbook -i inventory/hosts.yml playbooks/deploy.ymlSerial reboot all hosts in the inventory:
./reboot.zshPrune stale apt residue on all hosts:
./clean.zshTo limit execution to a single host or group:
ansible-playbook -i inventory/hosts.yml playbooks/deploy.yml --limit <hostname>For every host, the playbook performs the following steps:
- Facts — asserts the host is listed by fully qualified name, and sets the
datacenter(Nomad: this host's domain label) andconsul_datacenter(the Consul servers' domain label, orexisting_consul_datacenter) facts. - APT repository/update — adds the HashiCorp apt repository and updates/upgrades packages.
- Docker (optional,
docker.enabled: true) — installs Docker Engine, enables the service, and addsansible_userto thedockergroup. - NFS mounts (optional,
nfs_mounts_shares) — mounts NFS shares fromnas_hostat the given mount points via/etc/fstab. - Consul — creates config/data directories, installs Consul via the HashiCorp apt repository, templates
consul.hclwith datacenter, node name, server/client mode, andretry_joinderived from inventory, validates it, and registers a systemd service. - Nomad — creates config/data directories, installs Nomad via the HashiCorp apt repository, templates
nomad.hcl(includingbootstrap_expect,retry_join, thedockerplugin when enabled, and any declaredvolumesas host volumes), validates it, and registers a systemd service running as the non-rootnomaduser (seeroles/nomad/README.md).
Services are managed as systemd units (Nomad and Consul), and are only restarted when their config, package, or (Nomad only) data directory permissions/user override actually changed. Those restarts roll one host at a time — each restarted agent, and the server cluster's autopilot health, must report healthy before the next host is restarted, so a run that changes every server never takes more than one of them out of quorum.
- Platform — This playbook is tested against Ubuntu 24.04 LTS. Other Ubuntu versions may work but are untested.
- Companion project — Nomadintosh is the macOS counterpart to this playbook. Nomad's multi-platform support means both clusters can participate in the same datacenter if desired.
- Parent project — Nomadable composes this playbook with Nomadintosh into one deployment, dispatching each inventory host to the right child playbook by OS so a single mixed macOS/Ubuntu cluster can be deployed in one run. This is the actual deploy path in practice — running this repo's own
ansible-playbook/deploy.zshstandalone (as described above) still works for Ubuntu-only changes, but Nomadable is what's normally invoked.
