Infrastructure@Home: Using Ansible to Install the Service Monitoring Software Consul

  • AM4: An interface in which I see the status of each application (health, exposed endpoints)

Ansible Roles

- name: Install new role
- hosts: all
- roles:
- new_role
├── defaults
├── files
├── handlers
├── meta
├── tasks
├── templates
├── tests
└── vars


Install Consul with a Playbook

  • Determine the correct Linux distribution and hardware
  • Create consul group, user and directories
  • Download and link the consul binary
  • Copy configuration files
  • Enable Consul with systemd

Step 1 — Determine the correct Linux Distribution and Hardware

- name: Set vars when architecture is armv7l
consul_src_file: "consul_{{ consul_version }}"
consul_src_url: "{{ consul_version }}/consul_{{ consul_version }}"
consul_src_hash: "{{ consul_version }}/consul_{{ consul_version }}_SHA256SUMS"
when: ansible_facts.architecture == 'armv7l'

Step 2 — Create Consul Group, User and Directories

- name: Create consul group
name: consul
state: present
system: true
- name: Create consul user
name: consul
group: consul
shell: /bin/false
home: /etc/consul/
state: present
- name: Create consul dir
state: directory
path: "{{ consul_base_directory }}"
owner: consul
mode: 0755

Step 3 — Download and Link the Consul Binary

- name: Get consul binary
url: "{{ hostvars[inventory_hostname].consul_src_url }}"
dest: /tmp
checksum: sha256:{{ hostvars[inventory_hostname].consul_src_hash }}
- name: Unzip consul binary
src: /tmp/{{ hostvars[inventory_hostname].consul_src_file }}
dest: "{{ consul_base_directory }}"
remote_src: true
mode: 0744
- name: Create symlink
src: "{{ consul_base_directory }}/consul"
dest: /usr/local/bin/consul
state: link

Step 4 — Copy Configuration Files

"datacenter": "{{ consul_datacenter }}",
"data_dir": "{{ consul_base_directory }}",
"log_level": "INFO",
"enable_local_script_checks": true,
"enable_syslog": true,
"node_name": "{{ ansible_facts.hostname }}",
"retry_join": ["{{ consul_main_server_ip }}"]
- name: Copy consul config
src: config.json.j2
dest: "{{ consul_base_directory }}/config.json"
mode: 0644
notify: Restart consul
- name: Copy consul server config
src: server.json.j2
dest: "{{ consul_base_directory }}/server.json"
mode: 0644
when: consul_role == 'server'
notify: Restart consul

Step 5 — Enable Consul with Systemd

- name: Copy consul service file
src: consul.service.j2
dest: /etc/systemd/system/consul.service
mode: 0644
notify: Restart consul
- name: Set file permissions
path: "{{ consul_base_directory }}"
owner: consul
group: consul
recurse: true
- name: Enable consul service
service: consul.service
enabled: true
notify: Restart consul

Running the Playbook

Using Consul

$ consul —agent
==> Starting Consul agent…
Version: 'v1.5.2'
Node ID: '094163f6-9215-6f2c-c930-9e84600029da'
Node name: 'raspi-3-1+'
Datacenter: 'infrastructure_at_home' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
Cluster Addr: (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-E
$ consul membersNode       Address             Status  Type    Build  Protocol  DC                      Segment
raspi-3-2 alive server 1.7.1 2 infrastructure_at_home <all>
raspi-4-1 alive server 1.7.1 2 infrastructure_at_home <all>
raspi-4-2 alive server 1.7.1 2 infrastructure_at_home <all>
raspi-3-1 alive client 1.7.1 2 infrastructure_at_home <default>





Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store