-
Notifications
You must be signed in to change notification settings - Fork 2
/
prep_docker.yml
85 lines (67 loc) · 3.02 KB
/
prep_docker.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
---
- name: setup swarm cluster on manager and worker nodes
# =======================================================
hosts: all
gather_facts: true
become: true
vars:
distribution: "{{ ansible_distribution | lower }}"
swarm_port: 2377
tasks:
- name: ensure https apt transport exists
package:
name: '{{ item }}'
state: present
with_items:
- apt-transport-https
- ca-certificates
- name: add docker apt key
apt_key:
keyserver: 'hkp://p80.pool.sks-keyservers.net:80'
id: '9DC858229FC7DD38854AE2D88D81803C0EBFCD88'
- name: add docker apt repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
update_cache: yes
state: present
- name: install the docker package
package:
name: docker-ce
state: present
- name: ensure docker service will start on reboot
service: name=docker enabled=yes
- name: add vagrant user to docker group
user: append=yes groups=docker name=vagrant
- block:
- name: initialize a new swarm on manager node
command: docker swarm init --advertise-addr "{{ ansible_eth1["ipv4"]["address"] }}"
- name: get join worker token for swarm
command: docker swarm join-token worker -q
register: swarm_worker_token
- name: get join worker token for swarm
command: docker swarm join-token manager -q
register: swarm_manager_token
- name: set join worker token fact
set_fact: swarm_worker_token="{{ swarm_worker_token.stdout }}"
- name: set join manager token fact
set_fact: swarm_manager_token="{{ swarm_manager_token.stdout }}"
- name: fetch docker compose
get_url:
url: https://github.com/docker/compose/releases/download/1.13.0/docker-compose-Linux-x86_64
dest: /usr/bin/docker-compose
mode: 0755
when: inventory_hostname in ['m01'] # perform tasks only on manager node
- debug: var=hostvars['m01']['swarm_worker_token']
- debug: var=hostvars['m01']['swarm_manager_token']
- name: join worker nodes
command: docker swarm join --token "{{ hostvars['m01']['swarm_worker_token'] }}" "{{ hostvars['m01']['ansible_eth1']['ipv4']['address'] }}:2377"
when: inventory_hostname[0] == 'w'
- name: join manager nodes
command: docker swarm join --token "{{ hostvars['m01']['swarm_manager_token'] }}" "{{ hostvars['m01']['ansible_eth1']['ipv4']['address'] }}:2377"
when: inventory_hostname[0] == 'm' and inventory_hostname != 'm01'
- name: start the swarm visualizer on the manager node
command: docker run -it -d -p 3000:8080 -e HOST=m01 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer
when: inventory_hostname in ['m01']
- name: create an overlay network for a net001 app
command: docker network create net001 --driver overlay
when: inventory_hostname in ['m01']