This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
docker-ce.yaml
185 lines (158 loc) · 5.38 KB
/
docker-ce.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
- hosts: all
remote_user: root
become: yes
become_method: sudo
tasks:
- name: "add docker repository"
apt_repository: repo='deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable' state=present
when: ansible_distribution == "Ubuntu"
- name: "ensure old versions of docker are purged 1"
apt: name=lxc-docker state=absent purge=yes
when: ansible_distribution == "Ubuntu"
- name: "ensure old versions of docker are purged 2"
apt: name=docker state=absent purge=yes
when: ansible_distribution == "Ubuntu"
- name: "ensure old versions of docker are purged 3"
apt: name=docker-engine state=absent purge=yes
when: ansible_distribution == "Ubuntu"
- name: "ensure old versions of docker are purged 4"
apt: name=docker.io state=absent purge=yes
when: ansible_distribution == "Ubuntu"
- name: "get kernel version"
shell: uname -r
register: kernel
- name: "install 14.04 pre-req 1"
apt:
name: linux-image-extra-{{ kernel.stdout }}
state: present
update_cache: yes
install_recommends: yes
when: ansible_distribution == "Ubuntu"
retries: 3
delay: 20
- name: "install 14.04 pre-req 2"
apt: name=linux-image-extra-virtual state=present update_cache=yes install_recommends=yes
when: ansible_distribution == "Ubuntu"
retries: 3
delay: 20
- name: "install docker"
apt: name=docker-ce=17.06.2* state=present update_cache=yes install_recommends=yes allow_unauthenticated=yes
when: ansible_distribution == "Ubuntu"
retries: 3
delay: 20
- name: "add docker group"
group: name=docker state=present
- name: "add ubuntu to docker group"
user: name=ubuntu groups=docker append=yes
- name: "Restart Docker service"
service: name=docker state=started
- name: "get docker info"
shell: docker info
register: docker_info
changed_when: False
- hosts: docker-manager-first
remote_user: root
become: yes
become_method: sudo
tasks:
- name: "create primary swarm manager"
shell: docker swarm init --advertise-addr {{ ansible_eth0['ipv4']['address'] }}
when: "docker_info.stdout.find('Swarm: inactive') != -1"
- name: "get docker swarm manager token"
shell: docker swarm join-token -q manager
register: manager_token
- name: "get docker swarm worker token"
shell: docker swarm join-token -q worker
register: worker_token
- hosts: docker-managers
remote_user: root
become: yes
become_method: sudo
tasks:
- name: "join as a manager"
shell: "docker swarm join --token {{ hostvars['manager1']['manager_token']['stdout'] }} {{ hostvars['manager1']['ansible_eth0']['ipv4']['address'] }}:2377"
when: docker_info.stdout.find("Swarm{{':'}} inactive") != -1
retries: 3
delay: 20
- hosts: docker-workers
remote_user: root
become: yes
become_method: sudo
tasks:
- name: "join as a worker"
shell: "docker swarm join --token {{ hostvars['manager1']['worker_token']['stdout'] }} {{ hostvars['manager1']['ansible_eth0']['ipv4']['address'] }}:2377"
when: "docker_info.stdout.find('Swarm: inactive') != -1"
retries: 3
delay: 20
- hosts: docker-api
remote_user: root
become: yes
become_method: sudo
tasks:
- name: "Confirm Service Exists"
stat: path=/etc/init.d/docker
register: service_wrapper
- name: "Check whether api already exposed"
command: "grep 'DOCKER_OPTS=\"-D -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock\"' /etc/default/docker"
register: check_has_api
always_run: True
ignore_errors: True
changed_when: False
- name: "Stop Docker"
service: name=docker state=stopped
when:
- service_wrapper.stat.exists
- check_has_api.stdout == ""
register: service_stopped
- name: "expose docker api"
lineinfile: "dest=/etc/default/docker state=present regexp='#DOCKER_OPTS=' line='DOCKER_OPTS=\"-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock\"'"
when:
- service_stopped
- check_has_api.stdout == ""
- name: "Restart Docker service"
service: name=docker state=started
when:
- service_wrapper.stat.exists
- check_has_api.stdout == ""
- hosts: docker-cloud
remote_user: root
become: yes
become_method: sudo
tasks:
- name: "Confirm Service Exists"
stat: path=/etc/init.d/docker
register: service_wrapper
- name: "check for daemon file"
stat: path=/etc/docker/daemon.json
register: daemon_file
- name: "Stop Docker"
service: name=docker state=stopped
when:
- service_wrapper.stat.exists
- not daemon_file.stat.exists
register: service_stopped
- name: "create daemon file"
template: src=templates/daemon.j2 dest=/etc/docker/daemon.json
when: not daemon_file.stat.exists
- name: "Restart Docker service"
service: name=docker state=started
when:
- service_wrapper.stat.exists
- hosts: docker-manager-first
remote_user: root
become: yes
become_method: sudo
tasks:
- name: List networks
shell: docker network ls
register: docker_networks
- name: Create network when not there
shell: docker network create --driver overlay --subnet {{ item.subnet }} --gateway {{ item.gateway }} {{ item.name }}
with_items:
- {
name: 'caylent-default',
subnet: '17.0.0.0/16',
gateway: '17.0.0.1'
}
when: docker_networks.stdout.find( item.name ) == -1