-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-consul.yaml
88 lines (83 loc) · 2.65 KB
/
setup-consul.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
- hosts: server
vars:
ansible_user: ubuntu
inventory: hosts
tasks:
- name: Check server connection
command: echo "Connection is ready"
tags: ["check-ssh"]
## Update /etc/hosts
- name: Add IP hostname pair in /etc/hosts
lineinfile:
dest: /etc/hosts
state: present
regex: "{{ hostvars[item].ansible_default_ipv4.address }} {{ item }}"
line: "{{ hostvars[item].ansible_default_ipv4.address }} {{ item }}"
with_items: "{{ groups['server'] }}"
## Install Consul by APT
- name: Add Consul APT key
apt_key:
url: https://apt.releases.hashicorp.com/gpg
- name: Add Consul APT repository
apt_repository:
repo: deb https://apt.releases.hashicorp.com focal main
- name: Install Consul package
package:
name: consul
## Update Consul config file /etc/consul.d/consul.hcl
- name: Update Consul config, set server = true
lineinfile:
dest: /etc/consul.d/consul.hcl
state: present
regex: ".*server.*=.*"
line: "server = true"
- name: Update Consul config, set bootstrap_expect = 3
lineinfile:
dest: /etc/consul.d/consul.hcl
state: present
regex: ".*bootstrap_expect.*=.*"
line: "bootstrap_expect = 3"
- name: Update Consul config, set retry_join = [ALL_CONSUL_SERVERS]
lineinfile:
dest: /etc/consul.d/consul.hcl
state: present
regex: "^retry_join = .*"
line: >-
retry_join = ["{{ groups['server'] | join('", "') }}"]
- name: FIX Consul systemd service file
# There is a pitfall on Ubuntu 20.04 .deb package
# https://github.com/hashicorp/consul/issues/11911
lineinfile:
dest: /lib/systemd/system/consul.service
state: present
regex: "^EnvironmentFile=/etc/consul.d/consul.env"
line: "EnvironmentFile=-/etc/consul.d/consul.env"
## Start Consul services in the cluster
- name: Start Consul cluster
systemd:
name: consul
state: started
daemon_reload: yes
## Consul Web UI
- name: Enable Consul Web UI
command: 'echo "Enable Consul UI on {{ play_hosts[0] }}"'
when: inventory_hostname == play_hosts[0]
tags: ['consul-ui', 'never']
- name: Update Consul config, set ui_config.enable = true
replace:
path: /etc/consul.d/consul.hcl
backup: yes
regexp: "#ui_config{\n# enabled = true\n#}"
replace: |
ui_config {
enabled = true
}
when: inventory_hostname == play_hosts[0]
tags: ['consul-ui', 'never']
- name: Restart Consul server
systemd:
name: consul
state: restarted
daemon_reload: yes
when: inventory_hostname == play_hosts[0]
tags: ['consul-ui', 'never']