| 1 |
--- |
| 2 |
# Copyright (c) 2022 Felix Fontein <[email protected]> |
| 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 |
#################################################################### |
| 7 |
# WARNING: These are designed specifically for Ansible tests # |
| 8 |
# and should not be used as examples of how to write Ansible roles # |
| 9 |
#################################################################### |
| 10 |
|
| 11 |
- name: Create random nginx frontend name |
| 12 |
ansible.builtin.set_fact: |
| 13 |
daemon_nginx_frontend: '{{ "ansible-docker-test-daemon-frontend-%0x" % ((2**32) | random) }}' |
| 14 |
|
| 15 |
- block: |
| 16 |
- name: Create volume for config files |
| 17 |
community.docker.docker_volume: |
| 18 |
name: '{{ daemon_nginx_frontend }}' |
| 19 |
state: present |
| 20 |
|
| 21 |
- name: Create container for nginx frontend for daemon |
| 22 |
community.docker.docker_container: |
| 23 |
state: stopped |
| 24 |
name: '{{ daemon_nginx_frontend }}' |
| 25 |
image: "{{ docker_test_image_registry_nginx }}" |
| 26 |
volumes: |
| 27 |
- '{{ daemon_nginx_frontend }}:/etc/nginx/' |
| 28 |
- '/var/run/docker.sock:/var/run/docker.sock' |
| 29 |
network_mode: '{{ current_container_network_ip | default(omit, true) }}' |
| 30 |
networks: >- |
| 31 |
{{ |
| 32 |
[dict([['name', current_container_network_ip]])] |
| 33 |
if current_container_network_ip not in ['', 'bridge'] else omit |
| 34 |
}} |
| 35 |
register: nginx_container |
| 36 |
|
| 37 |
- name: Copy config files |
| 38 |
ansible.builtin.copy: |
| 39 |
src: "{{ item }}" |
| 40 |
dest: "{{ remote_tmp_dir }}/{{ item }}" |
| 41 |
mode: "0644" |
| 42 |
loop: |
| 43 |
- nginx.conf |
| 44 |
|
| 45 |
- name: Copy static files into volume |
| 46 |
community.docker.docker_container_copy_into: |
| 47 |
container: '{{ daemon_nginx_frontend }}' |
| 48 |
path: '{{ remote_tmp_dir }}/{{ item }}' |
| 49 |
container_path: '/etc/nginx/{{ item }}' |
| 50 |
owner_id: 0 |
| 51 |
group_id: 0 |
| 52 |
loop: |
| 53 |
- nginx.conf |
| 54 |
register: can_copy_files |
| 55 |
ignore_errors: true |
| 56 |
|
| 57 |
- when: can_copy_files is not failed |
| 58 |
block: |
| 59 |
|
| 60 |
- name: Create private keys |
| 61 |
community.crypto.openssl_privatekey: |
| 62 |
path: '{{ remote_tmp_dir }}/{{ item }}.key' |
| 63 |
type: ECC |
| 64 |
curve: secp256r1 |
| 65 |
force: true |
| 66 |
loop: |
| 67 |
- cert |
| 68 |
- ca |
| 69 |
|
| 70 |
- name: Create CSR for CA certificate |
| 71 |
community.crypto.openssl_csr: |
| 72 |
path: '{{ remote_tmp_dir }}/ca.csr' |
| 73 |
privatekey_path: '{{ remote_tmp_dir }}/ca.key' |
| 74 |
subject: |
| 75 |
commonName: Ansible test CA for Docker HTTPS connection tests |
| 76 |
useCommonNameForSAN: false |
| 77 |
basic_constraints: |
| 78 |
- 'CA:TRUE' |
| 79 |
basic_constraints_critical: true |
| 80 |
key_usage: |
| 81 |
- digitalSignature |
| 82 |
- Certificate Sign |
| 83 |
key_usage_critical: true |
| 84 |
extended_key_usage: |
| 85 |
- serverAuth # the same as "TLS Web Server Authentication" |
| 86 |
extended_key_usage_critical: true |
| 87 |
|
| 88 |
- name: Create CA certificate |
| 89 |
community.crypto.x509_certificate: |
| 90 |
path: '{{ remote_tmp_dir }}/ca.pem' |
| 91 |
csr_path: '{{ remote_tmp_dir }}/ca.csr' |
| 92 |
privatekey_path: '{{ remote_tmp_dir }}/ca.key' |
| 93 |
provider: selfsigned |
| 94 |
|
| 95 |
- name: Create CSR for frontend certificate |
| 96 |
community.crypto.openssl_csr: |
| 97 |
path: '{{ remote_tmp_dir }}/cert.csr' |
| 98 |
privatekey_path: '{{ remote_tmp_dir }}/cert.key' |
| 99 |
subject_alt_name: |
| 100 |
- DNS:daemon-tls.ansible.com |
| 101 |
subject_alt_name_critical: true |
| 102 |
|
| 103 |
- name: Create frontend certificate |
| 104 |
community.crypto.x509_certificate: |
| 105 |
path: '{{ remote_tmp_dir }}/cert.pem' |
| 106 |
csr_path: '{{ remote_tmp_dir }}/cert.csr' |
| 107 |
privatekey_path: '{{ remote_tmp_dir }}/cert.key' |
| 108 |
ownca_path: '{{ remote_tmp_dir }}/ca.pem' |
| 109 |
ownca_privatekey_path: '{{ remote_tmp_dir }}/ca.key' |
| 110 |
provider: ownca |
| 111 |
|
| 112 |
- name: Copy dynamic files into volume |
| 113 |
community.docker.docker_container_copy_into: |
| 114 |
container: '{{ daemon_nginx_frontend }}' |
| 115 |
path: '{{ remote_tmp_dir }}/{{ item }}' |
| 116 |
container_path: '/etc/nginx/{{ item }}' |
| 117 |
owner_id: 0 |
| 118 |
group_id: 0 |
| 119 |
loop: |
| 120 |
- ca.pem |
| 121 |
- cert.pem |
| 122 |
- cert.key |
| 123 |
|
| 124 |
- name: Start nginx frontend for daemon |
| 125 |
community.docker.docker_container: |
| 126 |
name: '{{ daemon_nginx_frontend }}' |
| 127 |
state: started |
| 128 |
register: nginx_container |
| 129 |
|
| 130 |
- name: Output nginx container network settings |
| 131 |
ansible.builtin.debug: |
| 132 |
var: nginx_container.container.NetworkSettings |
| 133 |
|
| 134 |
- name: Get proxied daemon URLs |
| 135 |
ansible.builtin.set_fact: |
| 136 |
# Since Docker 29, nginx_container.container.NetworkSettings.IPAddress no longer exists. |
| 137 |
# Use the bridge network's IP address instead... |
| 138 |
docker_daemon_frontend_https: >- |
| 139 |
https://{{ |
| 140 |
nginx_container.container.NetworkSettings.Networks[current_container_network_ip].IPAddress |
| 141 |
if current_container_network_ip else ( |
| 142 |
nginx_container.container.NetworkSettings.IPAddress |
| 143 |
| default(nginx_container.container.NetworkSettings.Networks['bridge'].IPAddress) |
| 144 |
) |
| 145 |
}}:5000 |
| 146 |
docker_daemon_frontend_http: >- |
| 147 |
http://{{ |
| 148 |
nginx_container.container.NetworkSettings.Networks[current_container_network_ip].IPAddress |
| 149 |
if current_container_network_ip else ( |
| 150 |
nginx_container.container.NetworkSettings.IPAddress |
| 151 |
| default(nginx_container.container.NetworkSettings.Networks['bridge'].IPAddress) |
| 152 |
) |
| 153 |
}}:6000 |
| 154 |
|
| 155 |
- name: Wait for registry frontend |
| 156 |
ansible.builtin.uri: |
| 157 |
url: '{{ docker_daemon_frontend_http }}/version' |
| 158 |
register: result |
| 159 |
until: result is success |
| 160 |
retries: 5 |
| 161 |
delay: 1 |
| 162 |
|
| 163 |
- name: Get docker daemon information directly |
| 164 |
community.docker.docker_host_info: |
| 165 |
register: output_direct |
| 166 |
|
| 167 |
- name: Show direct host info |
| 168 |
ansible.builtin.debug: |
| 169 |
var: output_direct.host_info | sanitize_host_info |
| 170 |
|
| 171 |
- name: Get docker daemon information via HTTP |
| 172 |
community.docker.docker_host_info: |
| 173 |
docker_host: '{{ docker_daemon_frontend_http }}' |
| 174 |
register: output_http |
| 175 |
|
| 176 |
- name: Show HTTP host info |
| 177 |
ansible.builtin.debug: |
| 178 |
var: output_http.host_info | sanitize_host_info |
| 179 |
|
| 180 |
- name: Check that information matches |
| 181 |
ansible.builtin.assert: |
| 182 |
that: |
| 183 |
- (output_direct.host_info | sanitize_host_info) == (output_http.host_info | sanitize_host_info) |
| 184 |
|
| 185 |
- name: Get docker daemon information via HTTPS |
| 186 |
community.docker.docker_host_info: |
| 187 |
docker_host: '{{ docker_daemon_frontend_https }}' |
| 188 |
tls_hostname: daemon-tls.ansible.com |
| 189 |
ca_cert: '{{ remote_tmp_dir }}/ca.pem' |
| 190 |
tls: true |
| 191 |
validate_certs: true |
| 192 |
register: output_https |
| 193 |
|
| 194 |
- name: Show HTTPS host info |
| 195 |
ansible.builtin.debug: |
| 196 |
var: output_https.host_info | sanitize_host_info |
| 197 |
|
| 198 |
- name: Check that information matches |
| 199 |
ansible.builtin.assert: |
| 200 |
that: |
| 201 |
- (output_direct.host_info | sanitize_host_info) == (output_https.host_info | sanitize_host_info) |
| 202 |
|
| 203 |
always: |
| 204 |
- name: Obtain logs from the nginx frontend |
| 205 |
ansible.builtin.command: docker logs {{ daemon_nginx_frontend }} |
| 206 |
register: output |
| 207 |
ignore_errors: true |
| 208 |
- ansible.builtin.debug: |
| 209 |
var: output.stdout_lines |
| 210 |
ignore_errors: true |
| 211 |
|
| 212 |
- name: Remove container |
| 213 |
community.docker.docker_container: |
| 214 |
state: absent |
| 215 |
name: '{{ daemon_nginx_frontend }}' |
| 216 |
force_kill: true |
| 217 |
ignore_errors: true |
| 218 |
|
| 219 |
- name: Remove volume |
| 220 |
community.docker.docker_volume: |
| 221 |
name: '{{ daemon_nginx_frontend }}' |
| 222 |
state: absent |
| 223 |
ignore_errors: true |