| 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 Podman |
| 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 Podman was already set up |
| 18 |
ansible.builtin.stat: |
| 19 |
path: /root/community.docker-podman-is-set-up |
| 20 |
register: podman_setup_marker |
| 21 |
|
| 22 |
- name: Figure out user ID |
| 23 |
ansible.builtin.command: id -u |
| 24 |
register: podman_user_id |
| 25 |
|
| 26 |
- when: not podman_setup_marker.stat.exists and not ansible_facts.module_running_in_container |
| 27 |
block: |
| 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 |
- when: has_podman |
| 50 |
block: |
| 51 |
- name: Install podman |
| 52 |
ansible.builtin.package: |
| 53 |
name: "{{ podman_packages }}" |
| 54 |
notify: cleanup podman |
| 55 |
|
| 56 |
- name: Start podman socket for this user |
| 57 |
ansible.builtin.systemd_service: |
| 58 |
name: "{{ podman_socket_service }}" |
| 59 |
state: started |
| 60 |
scope: "{{ 'global' if podman_user_id.stdout == '0' and ansible_facts.os_family == 'RedHat' and (ansible_facts.distribution_major_version | int < 10) else 'user' }}" |
| 61 |
environment: |
| 62 |
XDG_RUNTIME_DIR: "{{ '/run' if podman_user_id.stdout == '0' else ('/run/user/' ~ podman_user_id.stdout) }}" |
| 63 |
|
| 64 |
- name: Set marker that Podman was already set up |
| 65 |
ansible.builtin.file: |
| 66 |
path: /root/community.docker-podman-is-set-up |
| 67 |
state: touch |
| 68 |
when: podman_skip_cleanup |
| 69 |
|
| 70 |
- when: not has_podman or ansible_facts.module_running_in_container |
| 71 |
block: |
| 72 |
- ansible.builtin.set_fact: |
| 73 |
podman_cli_version: "0.0" |
| 74 |
podman_socket: "not available" |
| 75 |
|
| 76 |
- when: has_podman and not ansible_facts.module_running_in_container |
| 77 |
block: |
| 78 |
- name: Check Podman CLI version |
| 79 |
ansible.builtin.command: "podman version -f {% raw %}'{{.Client.Version}}'{% endraw %}" |
| 80 |
register: podman_cli_version_stdout |
| 81 |
ignore_errors: true |
| 82 |
|
| 83 |
- ansible.builtin.set_fact: |
| 84 |
podman_cli_version: "{{ podman_cli_version_stdout.stdout | default('0.0', true) }}" |
| 85 |
podman_socket: "unix://{{ '/run' if podman_user_id.stdout == '0' else ('/run/user/' ~ podman_user_id.stdout) }}/podman/podman.sock" |
| 86 |
|
| 87 |
- name: Create podman Docker context |
| 88 |
ansible.builtin.command: |
| 89 |
cmd: >- |
| 90 |
docker context |
| 91 |
create podman |
| 92 |
--description "Podman" |
| 93 |
--docker "host={{ podman_socket }}" |
| 94 |
ignore_errors: true |
| 95 |
|
| 96 |
- ansible.builtin.debug: |
| 97 |
msg: |- |
| 98 |
Podman CLI version: {{ podman_cli_version }} |
| 99 |
Podman socket: {{ podman_socket }} |
| 100 |
|
| 101 |
- when: podman_cli_version is version('0.0', '>') |
| 102 |
block: |
| 103 |
# Cleanup podman |
| 104 |
- name: Show all containers |
| 105 |
ansible.builtin.command: 'podman ps --no-trunc --format {% raw %}"{{.Names}}"{% endraw %}' |
| 106 |
|
| 107 |
- name: "Remove all ansible-docker-test-* podman containers" |
| 108 |
ansible.builtin.shell: 'podman ps --no-trunc --format {% raw %}"{{.Names}}"{% endraw %} | grep "^ansible-docker-test-" | xargs -r podman container rm -f' |
| 109 |
register: podman_containers |
| 110 |
retries: 3 |
| 111 |
delay: 3 |
| 112 |
until: podman_containers is success |
| 113 |
ignore_errors: true |
| 114 |
|
| 115 |
- name: "Remove all ansible-docker-test-* podman volumes" |
| 116 |
ansible.builtin.shell: 'podman volume ls --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-docker-test-" | xargs -r podman volume rm -f' |
| 117 |
register: podman_volumes |
| 118 |
ignore_errors: true |
| 119 |
|
| 120 |
- name: "Remove all ansible-docker-test-* podman networks" |
| 121 |
ansible.builtin.shell: 'podman network ls --no-trunc --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-docker-test-" | xargs -r podman network rm' |
| 122 |
register: podman_networks |
| 123 |
ignore_errors: true |
| 124 |
|
| 125 |
- name: Cleaned podman resources |
| 126 |
ansible.builtin.debug: |
| 127 |
var: podman_resources |
| 128 |
vars: |
| 129 |
podman_resources: |
| 130 |
containers: "{{ podman_containers.stdout_lines | default([]) }}" |
| 131 |
volumes: "{{ podman_volumes.stdout_lines | default([]) }}" |
| 132 |
networks: "{{ podman_networks.stdout_lines | default([]) }}" |
| 133 |
|
| 134 |
# List all existing podman resources |
| 135 |
- name: List all podman containers |
| 136 |
ansible.builtin.command: podman ps --no-trunc -a |
| 137 |
register: podman_containers |
| 138 |
ignore_errors: true |
| 139 |
|
| 140 |
- name: List all podman volumes |
| 141 |
ansible.builtin.command: podman volume ls |
| 142 |
register: podman_volumes |
| 143 |
ignore_errors: true |
| 144 |
|
| 145 |
- name: List all podman networks |
| 146 |
ansible.builtin.command: podman network ls --no-trunc |
| 147 |
register: podman_networks |
| 148 |
ignore_errors: true |
| 149 |
|
| 150 |
- name: List all podman images |
| 151 |
ansible.builtin.command: podman images --no-trunc -a |
| 152 |
register: podman_images |
| 153 |
ignore_errors: true |
| 154 |
|
| 155 |
- name: Still existing podman resources |
| 156 |
ansible.builtin.debug: |
| 157 |
var: podman_resources |
| 158 |
vars: |
| 159 |
podman_resources: |
| 160 |
containers: "{{ podman_containers.stdout_lines | default([]) }}" |
| 161 |
volumes: "{{ podman_volumes.stdout_lines | default([]) }}" |
| 162 |
networks: "{{ podman_networks.stdout_lines | default([]) }}" |
| 163 |
images: "{{ podman_images.stdout_lines | default([]) }}" |