docker_storage_cleanup.yml 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ---
  2. # This playbook attempts to cleanup unwanted docker files to help alleviate docker disk space issues.
  3. #
  4. # To run:
  5. #
  6. # 1. run the playbook:
  7. #
  8. # ansible-playbook -e 'cli_tag_name=<tag-name>' docker_storage_cleanup.yml
  9. #
  10. # Example:
  11. #
  12. # ansible-playbook -e 'cli_tag_name=ops-node-compute-12345' docker_storage_cleanup.yml
  13. #
  14. # Notes:
  15. # * This *should* not interfere with running docker images
  16. #
  17. - name: Clean up Docker Storage
  18. gather_facts: no
  19. hosts: "tag_Name_{{ cli_tag_name }}"
  20. user: root
  21. connection: ssh
  22. pre_tasks:
  23. - fail:
  24. msg: "This playbook requires {{item}} to be set."
  25. when: item is not defined or item == ''
  26. with_items:
  27. - cli_tag_name
  28. - name: Ensure docker is running
  29. service:
  30. name: docker
  31. state: started
  32. enabled: yes
  33. - name: Get docker info
  34. command: docker info
  35. register: docker_info
  36. - name: Show docker info
  37. debug:
  38. var: docker_info.stdout_lines
  39. - name: Remove exited and dead containers
  40. shell: "docker ps -a | awk '/Exited|Dead/ {print $1}' | xargs --no-run-if-empty docker rm"
  41. ignore_errors: yes
  42. - name: Remove dangling docker images
  43. shell: "docker images -q -f dangling=true | xargs --no-run-if-empty docker rmi"
  44. ignore_errors: yes
  45. - name: Remove non-running docker images
  46. shell: "docker images | grep -v -e registry.access.redhat.com -e docker-registry.usersys.redhat.com -e docker-registry.ops.rhcloud.com | awk '{print $3}' | xargs --no-run-if-empty docker rmi 2>/dev/null"
  47. ignore_errors: yes
  48. # leaving off the '-t' for docker exec. With it, it doesn't work with ansible and tty support
  49. - name: update zabbix docker items
  50. command: docker exec -i oso-rhel7-host-monitoring /usr/local/bin/cron-send-docker-metrics.py
  51. # Get and show docker info again.
  52. - name: Get docker info
  53. command: docker info
  54. register: docker_info
  55. - name: Show docker info
  56. debug:
  57. var: docker_info.stdout_lines