Skip to content

Commit

Permalink
Update playbook to initialize an actual swarm (and join worker nodes)
Browse files Browse the repository at this point in the history
  • Loading branch information
drmalex07 committed Sep 12, 2017
1 parent e841a50 commit a6c9b8e
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions play.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
---

#
# 1. Apply docker-engine role to all hosts
#

- hosts: all

pre_tasks:

- name: Check that a proper ipv4 network is specified
assert:
that: (network is defined) and (network | ipaddr("network"))

- name: Check primary ipv4 address falls into specified network
assert:
that: (ipv4_address is defined) and (ipv4_address | ipaddr(network))
that:
- (network is defined) and (network | ipaddr("network"))
- (ipv4_address is defined) and (ipv4_address | ipaddr(network))

- set_fact:
primary_ipv4_address: "{{ansible_all_ipv4_addresses| ipaddr(network)| first}}"
Expand All @@ -37,6 +39,7 @@
tasks:

- debug: var=play_hosts

#- debug: var=network
#- debug: var=hostname

Expand All @@ -51,3 +54,41 @@
insertafter: '^127\.0\.1\.1 .*'
with_items: '{{play_hosts}}'
sudo: yes


#
# 2. Initialize manager as a swarm manager node
#

- hosts: manager

tasks:

- name: Initialize swarm mode
shell: docker swarm init --advertise-addr {{primary_ipv4_address}}

- name: Get swarm token for joining a worker node
shell: docker swarm join-token -q worker
register: swarm_join_token_result

- set_fact:
swarm_join_token: '{{swarm_join_token_result.stdout}}'

- debug:
msg: The join token is {{swarm_join_token}}

#
# 3. Join worker nodes into swarm
#

- hosts: workers

tasks:

- set_fact:
swarm_join_token: '{{hostvars["manager"].swarm_join_token}}'
swarm_manager_address: '{{hostvars["manager"].primary_ipv4_address}}'

- name: Join into swarm
shell: docker swarm join --token "{{swarm_join_token}}" "{{swarm_manager_address}}:2377"

0 comments on commit a6c9b8e

Please sign in to comment.