Wrap up basic consul server

This commit is contained in:
Amarpreet Minhas 2020-08-21 00:26:07 -04:00
parent aabc5936b7
commit 8988e78e39
8 changed files with 152 additions and 13 deletions

View file

@ -0,0 +1,3 @@
---
consul_dc: columbia
...

View file

@ -0,0 +1,3 @@
---
consul_dc: columbia
...

View file

@ -0,0 +1,3 @@
---
consul_dc: columbia
...

View file

@ -0,0 +1,15 @@
[Unit]
Description=Consul Service Discovery Agent
After=network-online.target
[Service]
Type=simple
Restart=on-failure
User=consul
Group=consul
RestartSec=3
StateDirectory=consul
ExecStart=/usr/local/bin/consul agent -config-dir /etc/consul.d/
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1,14 @@
---
- name: daemon_reload
systemd:
daemon_reload: True
- name: restart_consul_debian
systemd:
name: consul
state: restarted
- name: restart_consul_fbsd
service:
name: consul
state: restarted

View file

@ -1,4 +1,33 @@
---
- name: ensure consul group
group:
name: consul
state: present
system: True
- name: ensure consul user
user:
name: consul
state: present
group: consul
system: True
- name: ensure consul config dir
file:
path: /etc/consul.d/
state: directory
owner: consul
group: consul
mode: 0755
- name: ensure consul data dir
file:
path: /opt/consul
state: directory
owner: consul
group: consul
mode: 0755
- name: check consul version
shell:
cmd: "consul --version | head -1 | cut -d'v' -f2"
@ -6,6 +35,7 @@
executable: /bin/bash
changed_when: False
register: installed_consul_version
check_mode: false
- name: get consul
unarchive:
@ -14,5 +44,29 @@
mode: 0755
owner: root
group: root
remote_src: yes
remote_src: True
when: installed_consul_version.stdout != consul_version
- name: copy consul unit file
copy:
src: files/consul.service
dest: /etc/systemd/system/consul.service
mode: 0755
owner: root
group: root
notify: daemon_reload
- name: template consul config
template:
src: templates/consul.hcl.j2
dest: /etc/consul.d/consul.hcl
owner: root
group: root
mode: 0755
notify: restart_consul_debian
- name: ensure consul is started and enabled
systemd:
name: consul
state: started
enabled: True

View file

@ -1,4 +1,33 @@
---
- name: ensure consul group
group:
name: consul
state: present
system: True
- name: ensure consul user
user:
name: consul
state: present
group: consul
system: True
- name: ensure consul config dir
file:
path: /usr/local/etc/consul.d/
state: directory
owner: consul
group: consul
mode: 0755
- name: ensure consul data dir
file:
path: /opt/consul
state: directory
owner: consul
group: consul
mode: 0755
- name: check consul version
shell:
cmd: "consul --version | head -1 | cut -d'v' -f2"
@ -7,20 +36,24 @@
changed_when: False
failed_when: False
register: installed_consul_version
check_mode: false
- name: get consul
get_url:
url: "https://releases.hashicorp.com/consul/{{ consul_version }}/consul_{{ consul_version }}_freebsd_amd64.zip"
dest: /tmp/consul.zip
when: ("No such file" not in installed_consul_version.msg) or (installed_consul_version.msg != consul_version)
register: get_consul
pkgng:
name: consul-{{ consul_version }}
state: present
- name: get consul
unarchive:
src: /tmp/consul.zip
dest: /usr/local/bin/
remote_src: True
mode: 0755
- name: template consul config
template:
src: templates/consul.hcl.j2
dest: /usr/local/etc/consul.d/consul.hcl
owner: root
group: staff
when: get_consul.changed
mode: 0755
notify: restart_consul_fbsd
- name: enable and start consul
service:
name: consul
state: started
enabled: yes

View file

@ -0,0 +1,14 @@
datacenter = "{{ consul_dc }}"
bind_addr = "{{ ansible_default_ipv4.address }}"
start_join = ["{{ groups['consul_server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join('","') }}"]
data_dir = "/opt/consul"
log_level = "INFO"
server = true
bootstrap_expect = 3
ui = true
addresses {
http = "0.0.0.0"
}
performance {
raft_multiplier = 1
}