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
new_role
├── README.md
├── defaults
├── files
├── handlers
├── meta
├── tasks
├── templates
├── tests
└── vars

Consul

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
set_fact:
consul_src_file: "consul_{{ consul_version }}_linux_armhfv6.zip"
consul_src_url: "https://releases.hashicorp.com/consul/{{ consul_version }}/consul_{{ consul_version }}_linux_armhfv6.zip"
consul_src_hash: "https://releases.hashicorp.com/consul/{{ consul_version }}/consul_{{ consul_version }}_SHA256SUMS"
when: ansible_facts.architecture == 'armv7l'

Step 2 — Create Consul Group, User and Directories

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

Step 3 — Download and Link the Consul Binary

- name: Get consul binary
get_url:
url: "{{ hostvars[inventory_hostname].consul_src_url }}"
dest: /tmp
checksum: sha256:{{ hostvars[inventory_hostname].consul_src_hash }}
- name: Unzip consul binary
unarchive:
src: /tmp/{{ hostvars[inventory_hostname].consul_src_file }}
dest: "{{ consul_base_directory }}"
remote_src: true
mode: 0744
- name: Create symlink
file:
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
template:
src: config.json.j2
dest: "{{ consul_base_directory }}/config.json"
mode: 0644
notify: Restart consul
- name: Copy consul server config
template:
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
template:
src: consul.service.j2
dest: /etc/systemd/system/consul.service
mode: 0644
notify: Restart consul
- name: Set file permissions
file:
path: "{{ consul_base_directory }}"
owner: consul
group: consul
recurse: true
- name: Enable consul service
systemd:
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: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
Cluster Addr: 127.0.0.1 (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 192.168.2.202:8301 alive server 1.7.1 2 infrastructure_at_home <all>
raspi-4-1 192.168.2.203:8301 alive server 1.7.1 2 infrastructure_at_home <all>
raspi-4-2 192.168.2.204:8301 alive server 1.7.1 2 infrastructure_at_home <all>
raspi-3-1 192.168.2.201:8301 alive client 1.7.1 2 infrastructure_at_home <default>

Conclusion

Footnotes

--

--

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