forked from rbales79/Ansible-Swarm-Deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarm-facts.yml
119 lines (107 loc) · 4.22 KB
/
swarm-facts.yml
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
---
# determine the status of each manager node and break them
# into two groups:
# - swarm_manager_operational (swarm is running and active)
# - swarm_manager_bootstrap (host needs to be joined to the cluster)
- hosts: manager
become: true
tasks:
- name: load docker info as facts
docker_info_facts:
- name: create swarm_manager_operational group
add_host:
hostname: "{{ item }}"
groups: swarm_manager_operational
with_items: "{{ ansible_play_hosts | default(play_hosts) }}"
when: "'{{ hostvars[item]['docker_info']['Swarm']['LocalNodeState'] }}' == 'active'"
run_once: true
- name: create swarm_manager_bootstrap group
add_host:
hostname: "{{ item }}"
groups: swarm_manager_bootstrap
with_items: "{{ ansible_play_hosts | default(play_hosts) }}"
when: "'{{ hostvars[item]['docker_info']['Swarm']['LocalNodeState'] }}' != 'active'"
run_once: true
# determine the status of each worker node and break them
# into two groups:
# - swarm_worker_operational (host is joined to the swarm cluster)
# - swarm_worker_bootstrap (host needs to be joined to the cluster)
- hosts: worker
become: true
tasks:
- name: load docker info as facts
docker_info_facts:
- name: create swarm_worker_operational group
add_host:
hostname: "{{ item }}"
groups: swarm_worker_operational
with_items: "{{ ansible_play_hosts | default(play_hosts) }}"
when: "'{{ hostvars[item]['docker_info']['Swarm']['LocalNodeState'] }}' == 'active'"
run_once: true
- name: create swarm_worker_bootstrap group
add_host:
hostname: "{{ item }}"
groups: swarm_worker_bootstrap
with_items: "{{ ansible_play_hosts | default(play_hosts) }}"
when: "'{{ hostvars[item]['docker_info']['Swarm']['LocalNodeState'] }}' != 'active'"
run_once: true
# when the swarm_manager_operational group is empty, meaning there
# are no hosts running swarm, we need to initialize one of the hosts
# then add it to the swarm_manager_operational group
- hosts: swarm_manager_bootstrap[0]
become: true
tasks:
- name: initialize swarm cluster
shell: >
docker swarm init
--advertise-addr={{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}:2377
when: "'swarm_manager_operational' not in groups"
register: bootstrap_first_node
- name: add initialized host to swarm_manager_operational group
add_host:
hostname: "{{ item }}"
groups: swarm_manager_operational
with_items: "{{ ansible_play_hosts | default(play_hosts) }}"
when: bootstrap_first_node | changed
# retrieve the swarm tokens and populate a list of ips listening on
# the swarm port 2377
- hosts: swarm_manager_operational[0]
become: true
vars:
iface: "{{ swarm_iface | default('eth0') }}"
tasks:
- name: retrieve swarm manager token
shell: docker swarm join-token -q manager
register: swarm_manager_token
- name: retrieve swarm worker token
shell: docker swarm join-token -q worker
register: swarm_worker_token
- name: populate list of manager ips
add_host:
hostname: "{{ hostvars[item]['ansible_' + iface]['ipv4']['address'] }}"
groups: swarm_manager_ips
with_items: "{{ ansible_play_hosts | default(play_hosts) }}"
# join the hosts not yet initialized to the swarm cluster
- hosts: swarm_manager_bootstrap:!swarm_manager_operational
become: true
vars:
token: "{{ hostvars[groups['swarm_manager_operational'][0]]['swarm_manager_token']['stdout'] }}"
tasks:
- name: join manager nodes to cluster
shell: >
docker swarm join
--advertise-addr={{ swarm_iface | default('eth0') }}:2377
--token={{ token }}
{{ groups['swarm_manager_ips'][0] }}:2377
# join the remaining workers to the swarm cluster
- hosts: swarm_worker_bootstrap
become: true
vars:
token: "{{ hostvars[groups['swarm_manager_operational'][0]]['swarm_worker_token']['stdout'] }}"
tasks:
- name: join worker nodes to cluster
shell: >
docker swarm join
--advertise-addr={{ swarm_iface | default('eth0') }}:2377
--token={{ token }}
{{ groups['swarm_manager_ips'][0] }}:2377