Debian Codegraph @

ansible_collections/community/docker/tests/integration/targets/setup_docker/tasks/main.yml

raw
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 ####################################################################
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: Setup Docker
12 when: ansible_facts.distribution ~ ansible_facts.distribution_major_version not in ['CentOS6', 'RedHat6']
13 block:
14 - name: Detect whether we are running inside a container
15 community.docker.current_container_facts:
16
17 - name: Look for marker whether Docker was already set up
18 ansible.builtin.stat:
19 path: /root/community.docker-docker-is-set-up
20 register: docker_setup_marker
21
22 - when: not docker_setup_marker.stat.exists
23 block:
24 - name: Determine whether Docker Daemon needs to be installed
25 ansible.builtin.set_fact:
26 needs_docker_daemon: '{{ not ansible_facts.module_running_in_container }}'
27
28 - name: Print information
29 ansible.builtin.debug:
30 msg: |-
31 OS family: {{ ansible_facts.os_family }}
32 Distribution: {{ ansible_facts.distribution }}
33 Distribution major version: {{ ansible_facts.distribution_major_version }}
34 Distribution full version: {{ ansible_facts.distribution_version }}
35
36 - name: Include distribution specific variables
37 ansible.builtin.include_vars: "{{ lookup('first_found', params) }}"
38 vars:
39 params:
40 files:
41 - "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml"
42 - "{{ ansible_facts.os_family }}-{{ ansible_facts.distribution_major_version }}.yml"
43 - "{{ ansible_facts.distribution }}.yml"
44 - "{{ ansible_facts.os_family }}.yml"
45 - default.yml
46 paths:
47 - "{{ role_path }}/vars"
48
49 - name: Include distribution specific tasks
50 ansible.builtin.include_tasks: "{{ lookup('first_found', params) }}"
51 vars:
52 params:
53 files:
54 - "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml"
55 - "{{ ansible_facts.os_family }}-{{ ansible_facts.distribution_major_version }}.yml"
56 - "{{ ansible_facts.distribution }}.yml"
57 - "{{ ansible_facts.os_family }}.yml"
58 paths:
59 - "{{ role_path }}/tasks"
60
61 - name: Make sure that docker is running
62 ansible.builtin.service:
63 name: docker
64 state: started
65 when: not ansible_facts.module_running_in_container
66
67 - name: Set marker that Docker was already set up
68 ansible.builtin.file:
69 path: /root/community.docker-docker-is-set-up
70 state: touch
71 when: docker_skip_cleanup
72
73 - name: Retrieve more information (0/4)
74 ansible.builtin.command:
75 cmd: sh -c export
76
77 - name: Retrieve more information (1/4)
78 ansible.builtin.command:
79 cmd: docker version
80
81 - name: Retrieve more information (2/4)
82 ansible.builtin.command:
83 cmd: docker info
84
85 - name: Retrieve more information (3/4)
86 ansible.builtin.command:
87 cmd: docker context ls
88
89 - name: Retrieve more information (4/4)
90 ansible.builtin.command:
91 cmd: which docker
92
93 # Detect docker API version
94 - name: Check Docker API version
95 ansible.builtin.command: "docker version -f {% raw %}'{{(index .Server.Components 0).Details.ApiVersion}}'{% endraw %}"
96 register: docker_api_version_stdout
97 ignore_errors: true
98
99 # Detect docker CLI and docker-py versions
100 - name: Check Docker CLI version
101 ansible.builtin.command: "docker version -f {% raw %}'{{.Client.Version}}'{% endraw %}"
102 register: docker_cli_version_stdout
103 ignore_errors: true
104
105 - ansible.builtin.set_fact:
106 docker_cli_version: "{{ docker_cli_version_stdout.stdout | default('0.0') }}"
107 docker_api_version: "{{ docker_api_version_stdout.stdout | default('0.0') }}"
108
109 - ansible.builtin.debug:
110 msg: "Docker CLI version: {{ docker_cli_version }}; Docker API version: {{ docker_api_version }}"
111
112 - block:
113 # Cleanup docker daemon
114 - name: Show all containers
115 ansible.builtin.command: 'docker ps --no-trunc --format {% raw %}"{{.Names}}"{% endraw %}'
116
117 - name: "Remove all ansible-docker-test-* docker containers"
118 ansible.builtin.shell: 'docker ps --no-trunc --format {% raw %}"{{.Names}}"{% endraw %} | grep "^ansible-docker-test-" | xargs -r docker rm -f'
119 register: docker_containers
120 retries: 3
121 delay: 3
122 until: docker_containers is success
123 ignore_errors: true
124
125 - name: "Remove all ansible-docker-test-* docker volumes"
126 ansible.builtin.shell: 'docker volume ls --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-docker-test-" | xargs -r docker volume rm -f'
127 register: docker_volumes
128 ignore_errors: true
129
130 - name: "Remove all ansible-docker-test-* docker networks"
131 ansible.builtin.shell: 'docker network ls --no-trunc --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-docker-test-" | xargs -r docker network rm'
132 register: docker_networks
133 ignore_errors: true
134
135 - name: Cleaned docker resources
136 ansible.builtin.debug:
137 var: docker_resources
138 vars:
139 docker_resources:
140 containers: "{{ docker_containers.stdout_lines | default([]) }}"
141 volumes: "{{ docker_volumes.stdout_lines | default([]) }}"
142 networks: "{{ docker_networks.stdout_lines | default([]) }}"
143
144 # List all existing docker resources
145 - name: List all docker containers
146 ansible.builtin.command: docker ps --no-trunc -a
147 register: docker_containers
148 ignore_errors: true
149
150 - name: List all docker volumes
151 ansible.builtin.command: docker volume ls
152 register: docker_volumes
153 ignore_errors: true
154
155 - name: List all docker networks
156 ansible.builtin.command: docker network ls --no-trunc
157 register: docker_networks
158 ignore_errors: true
159
160 - name: List all docker images
161 ansible.builtin.command: docker images --no-trunc -a
162 register: docker_images
163 ignore_errors: true
164
165 - name: Still existing docker resources
166 ansible.builtin.debug:
167 var: docker_resources
168 vars:
169 docker_resources:
170 containers: "{{ docker_containers.stdout_lines | default([]) }}"
171 volumes: "{{ docker_volumes.stdout_lines | default([]) }}"
172 networks: "{{ docker_networks.stdout_lines | default([]) }}"
173 images: "{{ docker_images.stdout_lines | default([]) }}"
174
175 when: docker_cli_version is version('0.0', '>')