|
| 1 | +--- |
| 2 | +### |
| 3 | +# provision-02-docker-swarm playbook |
| 4 | +## |
| 5 | +# Set up the Swarm cluster and put it into the desired state |
| 6 | +# |
| 7 | +# Playbook inspired by https://github.com/nextrevision/ansible-swarm-playbook |
| 8 | +# Big thanks to John Patterson for his work! |
| 9 | +## |
| 10 | + |
| 11 | + |
| 12 | +# use dockerswarm_advertise_addr instead of iface when ethX has multiple IP addresses |
| 13 | +- hosts: all |
| 14 | + tasks: |
| 15 | + - name: define dockerswarm_advertise_addr_string (defined) - PROVIDED |
| 16 | + set_fact: |
| 17 | + dockerswarm_advertise_addr_string: "{{ dockerswarm_advertise_addr }}" |
| 18 | + when: dockerswarm_advertise_addr is defined |
| 19 | + - name: define dockerswarm_advertise_addr_string (defined) - DEFAULTED TO HOST IP |
| 20 | + set_fact: |
| 21 | + dockerswarm_advertise_addr_string: "{{ ansible_ssh_host }}" |
| 22 | + when: dockerswarm_advertise_addr is not defined |
| 23 | + |
| 24 | + - name: Get info on Docker Swarm |
| 25 | + community.docker.docker_swarm_info: |
| 26 | + ignore_errors: yes |
| 27 | + register: swarm_node_facts |
| 28 | + |
| 29 | + - name: Inform about basic flags |
| 30 | + ansible.builtin.debug: |
| 31 | + msg: | |
| 32 | + Was able to talk to docker daemon: {{ swarm_node_facts.can_talk_to_docker }} |
| 33 | + Docker in Swarm mode: {{ swarm_node_facts.docker_swarm_active }} |
| 34 | + This is a Manager node: {{ swarm_node_facts.docker_swarm_manager }} |
| 35 | +
|
| 36 | +# determine the status of each manager node and break them |
| 37 | +# into two groups: |
| 38 | +# - dockerswarm_manager_operational (swarm is running and active) |
| 39 | +# - dockerswarm_manager_bootstrap (host needs to be joined to the cluster) |
| 40 | +- hosts: all:&dockerswarm_manager |
| 41 | + tasks: |
| 42 | + |
| 43 | + - name: Get info on Docker Swarm |
| 44 | + community.docker.docker_swarm_info: |
| 45 | + ignore_errors: yes |
| 46 | + register: swarm_node_facts |
| 47 | + |
| 48 | + - name: create dockerswarm_manager_operational group |
| 49 | + add_host: |
| 50 | + hostname: "{{ item }}" |
| 51 | + groups: dockerswarm_manager_operational |
| 52 | + with_items: "{{ ansible_play_hosts | default(play_hosts) }}" |
| 53 | + when: swarm_node_facts.docker_swarm_active |
| 54 | + run_once: true |
| 55 | + changed_when: False |
| 56 | + |
| 57 | + - name: create dockerswarm_manager_bootstrap group |
| 58 | + add_host: |
| 59 | + hostname: "{{ item }}" |
| 60 | + groups: dockerswarm_manager_bootstrap |
| 61 | + with_items: "{{ ansible_play_hosts | default(play_hosts) }}" |
| 62 | + when: not swarm_node_facts.docker_swarm_active |
| 63 | + run_once: true |
| 64 | + changed_when: False |
| 65 | + |
| 66 | +# determine the status of each worker node and break them |
| 67 | +# into two groups: |
| 68 | +# - dockerswarm_worker_operational (host is joined to the swarm cluster) |
| 69 | +# - dockerswarm_worker_bootstrap (host needs to be joined to the cluster) |
| 70 | +- hosts: all:&dockerswarm_worker |
| 71 | + tasks: |
| 72 | + - name: create dockerswarm_worker_operational group |
| 73 | + add_host: |
| 74 | + hostname: "{{ item }}" |
| 75 | + groups: dockerswarm_worker_operational |
| 76 | + with_items: "{{ ansible_play_hosts | default(play_hosts) }}" |
| 77 | + when: swarm_node_facts.docker_swarm_active |
| 78 | + run_once: true |
| 79 | + changed_when: False |
| 80 | + |
| 81 | + - name: create dockerswarm_worker_bootstrap group |
| 82 | + add_host: |
| 83 | + hostname: "{{ item }}" |
| 84 | + groups: dockerswarm_worker_bootstrap |
| 85 | + with_items: "{{ ansible_play_hosts | default(play_hosts) }}" |
| 86 | + when: not swarm_node_facts.docker_swarm_active |
| 87 | + run_once: true |
| 88 | + changed_when: False |
| 89 | + |
| 90 | +# when the dockerswarm_manager_operational group is empty, meaning there |
| 91 | +# are no hosts running swarm, we need to initialize one of the hosts |
| 92 | +# then add it to the dockerswarm_manager_operational group |
| 93 | +- hosts: dockerswarm_manager_bootstrap[0] |
| 94 | + tasks: |
| 95 | + - name: initialize swarm cluster |
| 96 | + command: > |
| 97 | + docker swarm init |
| 98 | + --advertise-addr={{ dockerswarm_advertise_addr_string }}:2377 |
| 99 | + when: "'dockerswarm_manager_operational' not in groups" |
| 100 | + register: bootstrap_first_node |
| 101 | + |
| 102 | + - name: add initialized host to dockerswarm_manager_operational group # noqa 503 |
| 103 | + add_host: |
| 104 | + hostname: "{{ item }}" |
| 105 | + groups: dockerswarm_manager_operational |
| 106 | + with_items: "{{ ansible_play_hosts | default(play_hosts) }}" |
| 107 | + when: bootstrap_first_node.changed |
| 108 | + |
| 109 | +# retrieve the swarm tokens and populate a list of ips listening on |
| 110 | +# the swarm port 2377 |
| 111 | +- hosts: dockerswarm_manager_operational[0] |
| 112 | + vars: |
| 113 | + tasks: |
| 114 | + - name: retrieve swarm manager token |
| 115 | + command: docker swarm join-token -q manager |
| 116 | + register: dockerswarm_manager_token |
| 117 | + changed_when: False |
| 118 | + |
| 119 | + - name: retrieve swarm worker token |
| 120 | + command: docker swarm join-token -q worker |
| 121 | + register: dockerswarm_worker_token |
| 122 | + changed_when: False |
| 123 | + |
| 124 | + - name: populate list of manager ips from dockerswarm_advertise_addr |
| 125 | + add_host: |
| 126 | + hostname: "{{ dockerswarm_advertise_addr_string }}" |
| 127 | + groups: dockerswarm_manager_ips |
| 128 | + changed_when: False |
| 129 | + |
| 130 | + - name: Inform about basic flags |
| 131 | + ansible.builtin.debug: |
| 132 | + msg: | |
| 133 | + {{groups}} |
| 134 | +
|
| 135 | +# join the hosts not yet initialized to the swarm cluster |
| 136 | +- hosts: dockerswarm_manager_bootstrap:!dockerswarm_manager_operational |
| 137 | + vars: |
| 138 | + token: "{{ hostvars[groups.dockerswarm_manager_operational[0]]['dockerswarm_manager_token']['stdout'] }}" |
| 139 | + tasks: |
| 140 | + - name: join manager nodes to cluster # noqa 301 |
| 141 | + command: > |
| 142 | + docker swarm join |
| 143 | + --advertise-addr={{ dockerswarm_advertise_addr_string }}:2377 |
| 144 | + --token={{ token }} |
| 145 | + {{ groups.dockerswarm_manager_ips[0] }}:2377 |
| 146 | +
|
| 147 | +# join the remaining workers to the swarm cluster |
| 148 | +- hosts: dockerswarm_worker_bootstrap |
| 149 | + vars: |
| 150 | + token: "{{ hostvars[groups.dockerswarm_manager_operational[0]]['dockerswarm_worker_token']['stdout'] }}" |
| 151 | + tasks: |
| 152 | + - name: join worker nodes to cluster # noqa 301 |
| 153 | + command: > |
| 154 | + docker swarm join |
| 155 | + --advertise-addr={{ dockerswarm_advertise_addr_string }} |
| 156 | + --token={{ token }} |
| 157 | + {{ groups.dockerswarm_manager_ips[0] }}:2377 |
0 commit comments