63 lines
2.0 KiB
YAML

---
- name: Init swarm
when: inventory_hostname == groups.swarm_managers[0]
become: true
docker_swarm:
state: present
advertise_addr: "{{ansible_default_ipv4.address}}"
default_addr_pool:
- 172.17.0.0/16
- 172.18.0.0/16
- 172.19.0.0/16
register: manager_swarm_facts
- when: inventory_hostname == groups.swarm_managers[0]
set_fact:
worker_join_token: "{{manager_swarm_facts.swarm_facts.JoinTokens.Worker}}"
manager_join_token: "{{manager_swarm_facts.swarm_facts.JoinTokens.Manager}}"
- name: Create ingress network
# The ingress network conflicts with my subnet and ends up causing problems,
# so we have to set a different subnet first
when: inventory_hostname == groups.swarm_managers[0]
become: true
docker_network:
name: ingress
driver: overlay
ingress: true
scope: swarm
ipam_config:
- subnet: 172.31.0.0/16
gateway: 172.31.0.1
driver_options:
# I'm honestly not completely sure what this, but in the default
# ingress network that's created during swarm initialization, this exists
# and things don't seem to work without it.
com.docker.network.driver.overlay.vxlanid_list: 4096
- name: Join swarm as managers
when: inventory_hostname in groups.swarm_managers and inventory_hostname != groups.swarm_managers[0]
become: true
docker_swarm:
state: join
join_token: "{{hostvars[groups.swarm_managers[0]].manager_join_token}}"
advertise_addr: "{{ansible_default_ipv4.address}}"
remote_addrs: ["{{hostvars[groups.swarm_managers[0]].ansible_default_ipv4.address}}"]
- name: Join swarm as workers
when: inventory_hostname in groups.swarm_workers
become: true
docker_swarm:
state: join
join_token: "{{hostvars[groups.swarm_managers[0]].worker_join_token}}"
advertise_addr: "{{ansible_default_ipv4.address}}"
remote_addrs: ["{{hostvars[groups.swarm_managers[0]].ansible_default_ipv4.address}}"]
- name: Add user to docker group
become: true
user:
name: "{{ ansible_user }}"
groups:
- docker