Make role support multiple container runtimes

- add variable to be used for container runtime
- correct Docker container removal task  based on new output from docker_facts.py
- add validation for supported container runtimes
- update README

Change-Id: If9f8488192d0cc04783f9069dc43113da204ca57
This commit is contained in:
Sam Doran 2018-09-07 16:20:41 -04:00
parent fb5f4ce14d
commit 4fab777295
5 changed files with 30 additions and 13 deletions

View File

@ -17,20 +17,25 @@ To fetch logs with this role, use the `fetch_logs.yml` tasks file. By default, e
See `defaults/main.yml` for the dictionary of options to control logs that are fetched.
## Cleanup Docker ##
## Cleanup Container Items ##
**WARNING:** This will delete images, containers, and volumes from the target system(s).
To perform the most common cleanup tasks -- delete dangling images and volumes and delete exited or dead containers -- use the `cleanup_docker.yml` tasks file. This role includes a `docker_facts` module for enumerating images, volumes, and containers. The filtered lists (one each for images, containers, and volumes) returned by this module is used to determine which items to remove. The module accepts a list of `k=v` filter arguments that will be passed to the `-f` option of Docker. Specifying multiple filters creates an `and` match, so all filters must match.
To perform the most common cleanup tasks --- delete dangling images and volumes and delete exited or dead containers --- use the `container_cleanup.yml` tasks file.
See Docker guides for [images](https://docs.docker.com/engine/reference/commandline/images/#filtering), [containers](https://docs.docker.com/engine/reference/commandline/ps/#filtering), and [volumes](https://docs.docker.com/engine/reference/commandline/volume_ls/#filtering) for filter options.
This role includes modules for listing image, volume, and container IDs. The filtered lists (one each for images, containers, and volumes) returned by this module are used to determine which items to remove. Specifying multiple filters creates an `and` match, so all filters must match.
If using Docker, see these guides for [images](https://docs.docker.com/engine/reference/commandline/images/#filtering), [containers](https://docs.docker.com/engine/reference/commandline/ps/#filtering), and [volumes](https://docs.docker.com/engine/reference/commandline/volume_ls/#filtering) for filter options.
## Requirements ##
- ansible >= 2.4
- docker-py >= 1.7.0
- Docker API >= 1.20
- ansible >= 2.4
If using Docker:
- docker-py >= 1.7.0
- Docker API >= 1.20
## Role Variables ##
@ -39,6 +44,7 @@ See Docker guides for [images](https://docs.docker.com/engine/reference/commandl
| Name | Default Value | Description |
|-------------------|---------------------|----------------------|
| `operations_container_runtime` | `docker` | Container runtime to use. Currently supports `docker` and `podman`. |
| `operations_image_filter` | `['dangling=true']` | List of image filters. |
| `operations_volume_filter` | `['dangling=true']` | List of volume filters. |
| `operations_container_filter` | `['status=exited', 'status=dead']` | List of container filters. |
@ -80,23 +86,23 @@ None
- mariadb
### Cleanup Docker ###
### Cleanup Container Items ###
- name: Cleanup dangling and dead images, containers, and volumes
hosts: all
tasks:
- name: Cleanup unused Docker images, containers, and volumes
- name: Cleanup unused images, containers, and volumes
import_role:
name: openstack-operations
tasks_from: cleanup_docker.yml
tasks_from: container_cleanup.yml
- name: Use custom filters for cleaning
hosts: all
tasks:
- name: Cleanup unused Docker images, containers, and volumes
- name: Cleanup unused images, containers, and volumes
import_role:
name: openstack-operations
tasks_from: cleanup_docker.yml
tasks_from: container_cleanup.yml
vars:
operations_image_filters:
- before=image1
@ -108,6 +114,7 @@ None
### Fetch Logs ###
- hosts: all
tasks:
- name: Fetch logs

View File

@ -1,4 +1,5 @@
# Cleanup Docker
# Cleanup Container
operations_container_runtime: docker
operations_image_filter:
- dangling=true
operations_volume_filter:

View File

@ -0,0 +1,7 @@
- name: Ensure a valid container runtime is used
assert:
msg: Invalid container runtime specified. Only 'docker' and 'podman' are valid.
that:
- operations_container_runtime in ['docker', 'podman']
- include_tasks: "{{ operations_container_runtime }}.yml"

View File

@ -14,7 +14,7 @@
docker_container:
name: "{{ item }}"
state: absent
loop: "{{ docker.containers_filtered | map(attribute='id') | list }}"
loop: "{{ docker.containers_filtered }}"
- name: Remove dangling volumes
docker_volume:

2
tasks/podman.yml Normal file
View File

@ -0,0 +1,2 @@
- debug:
msg: Podman tasks here