2018-10-19 09:42:27 +02:00
|
|
|
---
|
|
|
|
- name: Registering container and network names
|
|
|
|
set_fact:
|
|
|
|
cname_1: "{{ name_prefix ~ '-container-1' }}"
|
|
|
|
cname_2: "{{ name_prefix ~ '-container-2' }}"
|
|
|
|
cname_3: "{{ name_prefix ~ '-container-3' }}"
|
|
|
|
nname_1: "{{ name_prefix ~ '-network-1' }}"
|
|
|
|
nname_2: "{{ name_prefix ~ '-network-2' }}"
|
|
|
|
- name: Registering container and network names
|
|
|
|
set_fact:
|
2019-05-31 23:47:53 +02:00
|
|
|
cnames: "{{ cnames + [cname_1, cname_2, cname_3] }}"
|
|
|
|
dnetworks: "{{ dnetworks + [nname_1, nname_2] }}"
|
2018-10-19 09:42:27 +02:00
|
|
|
|
|
|
|
- name: Create containers
|
|
|
|
docker_container:
|
|
|
|
name: "{{ container_name }}"
|
2018-11-05 13:55:27 +01:00
|
|
|
image: alpine:3.8
|
|
|
|
command: /bin/sleep 10m
|
|
|
|
state: started
|
2018-10-19 09:42:27 +02:00
|
|
|
loop:
|
|
|
|
- "{{ cname_1 }}"
|
|
|
|
- "{{ cname_2 }}"
|
|
|
|
- "{{ cname_3 }}"
|
|
|
|
loop_control:
|
|
|
|
loop_var: container_name
|
|
|
|
|
|
|
|
####################################################################
|
|
|
|
|
|
|
|
- name: Create network
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: present
|
|
|
|
register: networks_1
|
|
|
|
|
2018-10-29 22:34:00 +01:00
|
|
|
- name: Connect network to containers 1
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: present
|
|
|
|
connected:
|
|
|
|
- "{{ cname_1 }}"
|
|
|
|
register: networks_2
|
|
|
|
|
|
|
|
- name: Connect network to containers 1 (idempotency)
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: present
|
|
|
|
connected:
|
|
|
|
- "{{ cname_1 }}"
|
|
|
|
register: networks_2_idem
|
|
|
|
|
2018-10-19 09:42:27 +02:00
|
|
|
- name: Connect network to containers 1 and 2
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: present
|
|
|
|
connected:
|
|
|
|
- "{{ cname_1 }}"
|
|
|
|
- "{{ cname_2 }}"
|
2018-10-29 22:34:00 +01:00
|
|
|
register: networks_3
|
2018-10-19 09:42:27 +02:00
|
|
|
|
|
|
|
- name: Connect network to containers 1 and 2 (idempotency)
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: present
|
|
|
|
connected:
|
|
|
|
- "{{ cname_1 }}"
|
|
|
|
- "{{ cname_2 }}"
|
2018-10-29 22:34:00 +01:00
|
|
|
register: networks_3_idem
|
2018-10-19 09:42:27 +02:00
|
|
|
|
|
|
|
- name: Connect network to container 3
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: present
|
|
|
|
connected:
|
|
|
|
- "{{ cname_3 }}"
|
|
|
|
appends: yes
|
2018-10-29 22:34:00 +01:00
|
|
|
register: networks_4
|
2018-10-19 09:42:27 +02:00
|
|
|
|
|
|
|
- name: Connect network to container 3 (idempotency)
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: present
|
|
|
|
connected:
|
|
|
|
- "{{ cname_3 }}"
|
|
|
|
appends: yes
|
2018-10-29 22:34:00 +01:00
|
|
|
register: networks_4_idem
|
2018-10-19 09:42:27 +02:00
|
|
|
|
|
|
|
- name: Disconnect network from container 1
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: present
|
|
|
|
connected:
|
|
|
|
- "{{ cname_2 }}"
|
|
|
|
- "{{ cname_3 }}"
|
2018-10-29 22:34:00 +01:00
|
|
|
register: networks_5
|
2018-10-19 09:42:27 +02:00
|
|
|
|
|
|
|
- name: Disconnect network from container 1 (idempotency)
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: present
|
|
|
|
connected:
|
|
|
|
- "{{ cname_2 }}"
|
|
|
|
- "{{ cname_3 }}"
|
2018-10-29 22:34:00 +01:00
|
|
|
register: networks_5_idem
|
2018-10-19 09:42:27 +02:00
|
|
|
|
|
|
|
- name: Cleanup
|
|
|
|
docker_network:
|
|
|
|
name: "{{ nname_1 }}"
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- networks_1 is changed
|
|
|
|
- networks_2 is changed
|
2018-11-05 13:55:27 +01:00
|
|
|
- networks_2_idem is not changed
|
2018-10-19 09:42:27 +02:00
|
|
|
- networks_3 is changed
|
2018-11-05 13:55:27 +01:00
|
|
|
- networks_3_idem is not changed
|
2018-10-19 09:42:27 +02:00
|
|
|
- networks_4 is changed
|
2018-11-05 13:55:27 +01:00
|
|
|
- networks_4_idem is not changed
|
2018-10-29 22:34:00 +01:00
|
|
|
- networks_5 is changed
|
2018-11-05 13:55:27 +01:00
|
|
|
- networks_5_idem is not changed
|
2018-10-19 09:42:27 +02:00
|
|
|
|
|
|
|
####################################################################
|
|
|
|
|
|
|
|
- name: Delete containers
|
|
|
|
docker_container:
|
|
|
|
name: "{{ container_name }}"
|
|
|
|
state: absent
|
2018-11-16 13:37:12 +01:00
|
|
|
force_kill: yes
|
2018-10-19 09:42:27 +02:00
|
|
|
loop:
|
|
|
|
- "{{ cname_1 }}"
|
|
|
|
- "{{ cname_2 }}"
|
|
|
|
- "{{ cname_3 }}"
|
|
|
|
loop_control:
|
|
|
|
loop_var: container_name
|