Use custom docker_facts module to get Docker info
This commit is contained in:
parent
c732b72ce5
commit
f60191f1b4
24
README.md
24
README.md
@ -11,17 +11,10 @@ None
|
||||
Role Variables
|
||||
--------------
|
||||
|
||||
**General Variables**
|
||||
|
||||
| Name | Default Value | Description |
|
||||
|-------------------|---------------------|----------------------|
|
||||
| `operations_task` | `skip` | Task file to include and run. See `tasks/` for available options. |
|
||||
|
||||
**Variables used for cleaning up Docker**
|
||||
|
||||
| Name | Default Value | Description |
|
||||
|-------------------|---------------------|----------------------|
|
||||
| `operations_docker_bin` | `docker` | Path to `docker` binary. |
|
||||
| `operations_docker_cleanup` | [see `defaults/main.yml`] | Filters used to determine which items will be removed. Uses Docker filter syntax. 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. |
|
||||
|
||||
**Variables for fetching logs**
|
||||
@ -49,30 +42,23 @@ Example Playbook
|
||||
- name: Restart a service
|
||||
import_role:
|
||||
name: openstack-operations
|
||||
tasks_from: restart_service.yml
|
||||
vars:
|
||||
operations_task: restart_service
|
||||
operations_service_list:
|
||||
operations_service_names:
|
||||
- docker
|
||||
- keystone
|
||||
- mariadb
|
||||
|
||||
- name: Cleanup unused Docker images
|
||||
- name: Cleanup unused Docker images, containers, and volumes
|
||||
import_role:
|
||||
name: openstack-operations
|
||||
vars:
|
||||
operations_task: cleanup_images
|
||||
tasks_from: cleanup_docker.yml
|
||||
|
||||
- name: Fetch logs
|
||||
import_role:
|
||||
name: openstack-operations
|
||||
vars:
|
||||
operations_task: fetch_logs
|
||||
|
||||
- name: List running services
|
||||
import_role:
|
||||
name: list_services
|
||||
vars:
|
||||
operations_task: fetch_logs
|
||||
tasks_from: fetch_logs.yml
|
||||
|
||||
License
|
||||
-------
|
||||
|
@ -1,16 +1,12 @@
|
||||
operations_operation: skip
|
||||
|
||||
|
||||
# Cleanup Docker
|
||||
operations_docker_bin: docker
|
||||
operations_docker_cleanup:
|
||||
image_filters:
|
||||
- dangling=true
|
||||
volume_filters:
|
||||
- dangling=true
|
||||
container_filters:
|
||||
- status=exited
|
||||
- status=dead
|
||||
operations_image_filters:
|
||||
- dangling=true
|
||||
operations_volume_filters:
|
||||
- dangling=true
|
||||
operations_container_filters:
|
||||
- status=exited
|
||||
- status=dead
|
||||
|
||||
|
||||
# Fetch Logs
|
||||
|
76
library/docker_facts.py
Normal file
76
library/docker_facts.py
Normal file
@ -0,0 +1,76 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2018 Ansible Project
|
||||
# GNU General Public License v3.0+
|
||||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {
|
||||
'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'
|
||||
}
|
||||
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
image_filter=dict(type='list', default=[]),
|
||||
volume_filter=dict(type='list', default=[]),
|
||||
container_filter=dict(type='list', default=[]),
|
||||
)
|
||||
)
|
||||
|
||||
docker_bin = module.get_bin_path('docker', True)
|
||||
docker_facts = {}
|
||||
|
||||
# Images
|
||||
command = [docker_bin, 'images', '-q']
|
||||
command_opts = ['-f ' + i for i in module.params['image_filter']]
|
||||
command.extend(command_opts)
|
||||
rc, out, err = module.run_command(command)
|
||||
if out == '':
|
||||
images = []
|
||||
else:
|
||||
images = out.strip().split('\n')
|
||||
docker_facts['filtered_images'] = images
|
||||
|
||||
# Volumes
|
||||
command = [docker_bin, 'volume', 'ls', '-q']
|
||||
command_opts = ['-f ' + i for i in module.params['volume_filter']]
|
||||
command.extend(command_opts)
|
||||
rc, out, err = module.run_command(command)
|
||||
if out == '':
|
||||
volumes = []
|
||||
else:
|
||||
volumes = out.strip().split('\n')
|
||||
docker_facts['filtered_volumes'] = volumes
|
||||
|
||||
# Containers
|
||||
command = [docker_bin, 'ps', '-q']
|
||||
command_opts = ['-f ' + i for i in module.params['container_filter']]
|
||||
command.extend(command_opts)
|
||||
rc, out, err = module.run_command(command)
|
||||
if out == '':
|
||||
containers = []
|
||||
else:
|
||||
containers = out.strip().split('\n')
|
||||
docker_facts['filtered_containers'] = containers
|
||||
|
||||
results = dict(
|
||||
ansible_facts=dict(
|
||||
docker_facts=docker_facts
|
||||
)
|
||||
)
|
||||
|
||||
module.exit_json(**results)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
23
tasks/cleanup_docker.yml
Normal file
23
tasks/cleanup_docker.yml
Normal file
@ -0,0 +1,23 @@
|
||||
- name: Gather Docker facts
|
||||
docker_facts:
|
||||
image_filter: "{{ operations_image_filter }}"
|
||||
volume_filter: "{{ operations_volume_filter }}"
|
||||
container_filter: "{{ operations_container_filter }}"
|
||||
|
||||
- name: Remove images
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
loop: "{{ docker_facts.images }}"
|
||||
|
||||
- name: Remove containers
|
||||
docker_container:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
loop: "{{ docker_facts.containers }}"
|
||||
|
||||
- name: Remove dangling volumes
|
||||
docker_volume:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
loop: "{{ docker_facts.volumes }}"
|
@ -1,29 +0,0 @@
|
||||
- name: List filtered images
|
||||
command: '{{ operations_docker_bin }} images {% for filter in operations_docker_cleanup.image_filters %}-f {{ filter }} {% endfor %}-q'
|
||||
changed_when: no
|
||||
check_mode: no
|
||||
register: _dangling_images
|
||||
|
||||
- name: Remove images
|
||||
command: '{{ operations_docker_bin }} rmi {{ item }}'
|
||||
with_items: "{{ _dangling_images.stdout_lines }}"
|
||||
|
||||
- name: List filtered containers
|
||||
command: '{{ operations_docker_bin }} ps {% for filter in operations_docker_cleanup.container_filters %}-f {{ filter }} {% endfor %} -q'
|
||||
changed_when: no
|
||||
check_mode: no
|
||||
register: _dead_containers
|
||||
|
||||
- name: Remove containers
|
||||
command: '{{ operations_docker_bin }} rm {{ item }}'
|
||||
with_items: "{{ _dead_containers.stdout_lines }}"
|
||||
|
||||
- name: List filtered volumes
|
||||
command: '{{ operations_docker_bin }} volume ls {% for filter in operations_docker_cleanup.volume_filters %}-f {{ filter }} {% endfor %} -q'
|
||||
changed_when: no
|
||||
check_mode: no
|
||||
register: _dangling_volumes
|
||||
|
||||
- name: Remove dangling volumes
|
||||
command: "{{ operations_docker_bin }} volume rm {{ item }}"
|
||||
with_items: "{{ _dangling_volumes }}"
|
@ -14,5 +14,5 @@
|
||||
- name: Fetch logs and place in {{ operations_log_destination }}
|
||||
fetch:
|
||||
src: "{{ item.path }}"
|
||||
dest: "{{ operations_log_destination }}/{{ inventory_hostname }}"
|
||||
dest: "{{ operations_log_destination }}"
|
||||
with_items: "{{ _logs.files }}"
|
||||
|
@ -1,3 +0,0 @@
|
||||
- include_tasks: "{{ operations_task }}.yml"
|
||||
tags:
|
||||
- operations
|
Loading…
x
Reference in New Issue
Block a user