Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
187 lines (164 sloc) 5.85 KB
---
# install docker ce on all machines
# this particular setup is for ubuntu 16.04
- hosts: all:!raspberrypi.dojo.io
become: true
tasks:
- name: install dependencies
apt:
name: "{{ item }}"
state: present
update_cache: yes
with_items:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- name: add docker ce repo key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: add docker engine repo
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"
state: present
filename: docker
update_cache: yes
- name: install docker-ce
apt:
name: docker-ce
state: present
update_cache: yes
# Disable IPV6
- name: Add configs to /etc/sysctl.conf
lineinfile:
path: /etc/sysctl.conf
line: "{{ item }}"
with_items:
- net.ipv6.conf.all.disable_ipv6 = 1
- net.ipv6.conf.default.disable_ipv6 = 1
- net.ipv6.conf.lo.disable_ipv6 = 1
- name: Update kernel perams
command: sudo sysctl -p
# - name: install other docker tools
# apt:
# name: "{{ item }}"
# state: present
# with_items:
# # - docker-compose
# # pip uninstall docker-py; pip uninstall docker; pip install docker
# 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: determine swarm status
shell: >
docker info | egrep '^Swarm: ' | cut -d ' ' -f2
register: swarm_status
- name: create swarm_manager_operational group
add_host:
hostname: "{{ item }}"
groups: swarm_manager_operational
with_items: "{{ ansible_play_hosts | default(play_hosts) }}"
when: "'active' in hostvars[item].swarm_status.stdout_lines"
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: "'active' not in hostvars[item].swarm_status.stdout_lines"
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: determine swarm status
shell: >
docker info | egrep '^Swarm: ' | cut -d ' ' -f2
register: swarm_status
- name: create swarm_worker_operational group
add_host:
hostname: "{{ item }}"
groups: swarm_worker_operational
with_items: "{{ ansible_play_hosts | default(play_hosts) }}"
when: "'active' in hostvars[item].swarm_status.stdout_lines"
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: "'active' not in hostvars[item].swarm_status.stdout_lines"
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={{ swarm_iface | default('eth0') }}: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 manager 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 worker hosts not yet initialized 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
- hosts: manager
become: true
tasks:
- name: Set manager availability
shell: docker node update "{{ ansible_hostname }}" --availability "{{ swarm_availability }}"