Swarm config playbook
This commit is contained in:
parent
aded6b456b
commit
3bfd89e1b6
|
|
@ -1,6 +1,144 @@
|
|||
---
|
||||
- name: Configure swarms
|
||||
hosts: swarm
|
||||
- name: "Configure swarms"
|
||||
hosts: swarm1
|
||||
roles:
|
||||
- common
|
||||
- docker
|
||||
tasks:
|
||||
- name: "Prepare manager group"
|
||||
add_host:
|
||||
hostname: "{{ item }}"
|
||||
groups: manager_group
|
||||
with_items: "{{play_hosts}}"
|
||||
when: hostvars[item]['swarm_master']
|
||||
|
||||
- name: "Prepare worker group"
|
||||
add_host:
|
||||
hostname: "{{ item }}"
|
||||
groups: worker_group
|
||||
with_items: "{{play_hosts}}"
|
||||
when: not hostvars[item]['swarm_master']
|
||||
|
||||
|
||||
|
||||
# From https://medium.com/@cantrobot/deploying-docker-swarm-with-ansible-a991c1028427
|
||||
|
||||
- name: "Gather status of manager nodes"
|
||||
hosts: manager_group
|
||||
become_user: docker
|
||||
tasks:
|
||||
- name: "Load docker info as facts"
|
||||
community.docker.docker_container_info:
|
||||
|
||||
- name: "Create swarm_manager_operational group"
|
||||
add_host:
|
||||
hostname: "{{ item }}"
|
||||
groups: swarm_manager_operational
|
||||
with_items: "{{ play_hosts }}"
|
||||
when: "'{{ hostvars[item]['docker_info']['Swarm']['LocalNodeState'] }}' == 'active'"
|
||||
run_once: true
|
||||
|
||||
- name: "Create swarm_manager_bootstrap group"
|
||||
add_host:
|
||||
hostname: "{{ item }}"
|
||||
groups: swarm_manager_bootstrap
|
||||
with_items: "{{ play_hosts }}"
|
||||
when: "'{{ hostvars[item]['docker_info']['Swarm']['LocalNodeState'] }}' != 'active'"
|
||||
run_once: true
|
||||
|
||||
|
||||
|
||||
- name: "Do the same for workers"
|
||||
hosts: worker_group
|
||||
become_user: docker
|
||||
tasks:
|
||||
- name: "Load docker info as facts"
|
||||
community.docker.docker_container_info:
|
||||
|
||||
- name: "Create swarm_worker_operational group"
|
||||
add_host:
|
||||
hostname: "{{ item }}"
|
||||
groups: swarm_worker_operational
|
||||
with_items: "{{ play_hosts }}"
|
||||
when: "'{{ hostvars[item]['docker_info']['Swarm']['LocalNodeState'] }}' == 'active'"
|
||||
run_once: true
|
||||
|
||||
- name: "Create swarm_worker_bootstrap group"
|
||||
add_host:
|
||||
hostname: "{{ item }}"
|
||||
groups: swarm_worker_bootstrap
|
||||
with_items: "{{ play_hosts }}"
|
||||
when: "'{{ hostvars[item]['docker_info']['Swarm']['LocalNodeState'] }}' != 'active'"
|
||||
run_once: true
|
||||
|
||||
|
||||
|
||||
- name: "Bootstrap the swarm"
|
||||
hosts: swarm_manager_bootstrap[0]
|
||||
become_user: docker
|
||||
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: "{{ play_hosts[0] }}"
|
||||
groups: swarm_manager_operational
|
||||
when: bootstrap_first_node | changed
|
||||
|
||||
|
||||
|
||||
- name: "Retrieve the tokens"
|
||||
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: "{{ play_hosts }}"
|
||||
|
||||
|
||||
- name: "Join managers"
|
||||
# filtered from possible bootstrap
|
||||
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
|
||||
|
||||
|
||||
|
||||
- name: "Join workers"
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue