| 1 |
--- |
| 2 |
# Copyright (c) Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
- name: Register registry cleanup |
| 7 |
# This must be done **before** docker is set up (see next task), to ensure that the |
| 8 |
# registry is removed **before** docker itself is removed. This is necessary as the |
| 9 |
# registry and its frontends run as docker containers. |
| 10 |
ansible.builtin.command: 'true' |
| 11 |
notify: Remove test registry |
| 12 |
|
| 13 |
- name: Setup Docker |
| 14 |
# Please note that we do setup_docker here and not via meta/main.yml to avoid the problem that |
| 15 |
# our cleanup is called **after** setup_docker's cleanup has been called! |
| 16 |
ansible.builtin.include_role: |
| 17 |
name: setup_docker |
| 18 |
|
| 19 |
- name: Figure out current container's network IP |
| 20 |
ansible.builtin.include_role: |
| 21 |
name: setup_docker_current_container_network_ip |
| 22 |
|
| 23 |
- name: Create random name prefix and test registry name |
| 24 |
ansible.builtin.set_fact: |
| 25 |
docker_registry_container_name_registry: '{{ ''ansible-docker-test-registry-%0x'' % ((2**32) | random) }}' |
| 26 |
docker_registry_container_name_nginx: '{{ ''ansible-docker-test-registry-frontend-%0x'' % ((2**32) | random) }}' |
| 27 |
docker_registry_container_name_nginx2: '{{ ''ansible-docker-test-registry-frontend2-%0x'' % ((2**32) | random) }}' |
| 28 |
|
| 29 |
- name: Create image and container list |
| 30 |
ansible.builtin.set_fact: |
| 31 |
docker_registry_setup_inames: [] |
| 32 |
docker_registry_setup_cnames: |
| 33 |
- '{{ docker_registry_container_name_registry }}' |
| 34 |
- '{{ docker_registry_container_name_nginx }}' |
| 35 |
- '{{ docker_registry_container_name_nginx2 }}' |
| 36 |
docker_registry_setup_vnames: |
| 37 |
- '{{ docker_registry_container_name_nginx }}' |
| 38 |
- '{{ docker_registry_container_name_nginx2 }}' |
| 39 |
|
| 40 |
- ansible.builtin.debug: |
| 41 |
msg: Using test registry name {{ docker_registry_container_name_registry }} and nginx frontend names {{ docker_registry_container_name_nginx }} and {{ docker_registry_container_name_nginx2 }} |
| 42 |
|
| 43 |
- ansible.builtin.fail: msg="Too old docker version to set up docker registry!" |
| 44 |
when: not(docker_api_version is version('1.25', '>=')) and (ansible_facts.distribution != 'CentOS' or ansible_facts.distribution_major_version|int > 6) |
| 45 |
|
| 46 |
- when: docker_api_version is version('1.25', '>=') |
| 47 |
block: |
| 48 |
|
| 49 |
# Set up registry container |
| 50 |
- name: Start test registry |
| 51 |
community.docker.docker_container: |
| 52 |
name: '{{ docker_registry_container_name_registry }}' |
| 53 |
image: "{{ docker_test_image_registry }}" |
| 54 |
ports: 5000 |
| 55 |
network_mode: '{{ current_container_network_ip | default(omit, true) }}' |
| 56 |
# We need to define the alias `real-registry` here because the global `links` |
| 57 |
# option for the NGINX containers (see setup-frontend.yml) does not work when |
| 58 |
# using networks. |
| 59 |
networks: >- |
| 60 |
{{ |
| 61 |
[dict([['name', current_container_network_ip], ['aliases', ['real-registry']]])] |
| 62 |
if current_container_network_ip not in ['', 'bridge'] else omit |
| 63 |
}} |
| 64 |
register: registry_container |
| 65 |
|
| 66 |
- name: Get registry URL |
| 67 |
ansible.builtin.set_fact: |
| 68 |
registry_address: localhost:{{ registry_container.container.NetworkSettings.Ports['5000/tcp'].0.HostPort }} |
| 69 |
|
| 70 |
# Set up first nginx frontend for registry |
| 71 |
- ansible.builtin.include_tasks: setup-frontend.yml |
| 72 |
vars: |
| 73 |
docker_registry_container_name_frontend: '{{ docker_registry_container_name_nginx }}' |
| 74 |
|
| 75 |
- ansible.builtin.set_fact: |
| 76 |
registry_frontend_address: '{{ docker_registry_frontend_address }}' |
| 77 |
|
| 78 |
# Set up second nginx frontend for registry |
| 79 |
- ansible.builtin.include_tasks: setup-frontend.yml |
| 80 |
vars: |
| 81 |
docker_registry_container_name_frontend: '{{ docker_registry_container_name_nginx2 }}' |
| 82 |
|
| 83 |
- ansible.builtin.set_fact: |
| 84 |
registry_frontend2_address: '{{ docker_registry_frontend_address }}' |
| 85 |
|
| 86 |
# Print addresses for registry and frontends |
| 87 |
- ansible.builtin.debug: |
| 88 |
msg: "Registry available under {{ registry_address }}, NGINX frontends available under {{ registry_frontend_address }} and {{ registry_frontend2_address }}" |