
  # with_fileglob doesn't work for subdirectories, so use `ls` directly
- name: Get list of previously migrated macrocontainers
  shell: 'ls -1 {{ macrocontainer_storage_path }}'
  ignore_errors: yes
  register: ls_macrocontainer_storage

- name: Remove any previously migrated macrocontainers
  shell: "docker rm -f {{ item }}"
  with_items:
    - "{{ ls_macrocontainer_storage.stdout_lines }}"
  when: ls_macrocontainer_storage|succeeded

- name: Delete any previously migrated macrocontainer data
  file:
    path: '{{ item }}'
    state: absent
  with_items:
    - "{{ ls_macrocontainer_storage.stdout_lines }}"
  when: ls_macrocontainer_storage|succeeded

- name: Ensure macrocontainer data directory is present
  file:
    path: '{{ macrocontainer_storage_path }}'
    state: directory
    owner: vagrant
    group: vagrant

- name: Ensure macrocontainer data directory exists
  file:
    path: '{{ macrocontainer_storage_path }}'
    state: directory
    owner: vagrant
    group: vagrant

  # with_fileglob doesn't work for subdirectories, so use `ls` directly
- name: Get list of previously migrated macrocontainers
  shell: 'ls -1 {{ macrocontainer_storage_path }}'
  register: ls_macrocontainer_storage
  changed_when: ls_macrocontainer_storage.stdout != ""

  # docker doesn't seem to offer a simple "Does this container exist?" command
  # but a quiet filter only produces output if it matches, and the grep
  # only succeeds if the piped command produces output
- name: Find any corresponding macrocontainers
  shell: >
      docker ps -aq --filter "name={{ item }}"
  with_items:
    - '{{ ls_macrocontainer_storage.stdout_lines }}'
  register: ps_macrocontainers

- name: Remove any previously migrated macrocontainers
  shell: 'docker stop {{ item.item }} && docker rm -fv {{ item.item }}'
  with_items: '{{ ps_macrocontainers.results }}'
  when: item.stdout

- name: Delete any previously migrated macrocontainer data
  file:
    path: '{{ macrocontainer_storage_path }}/{{ item }}'
    state: absent
  with_items:
    - '{{ ls_macrocontainer_storage.stdout_lines }}'

Fix various issues in macrocontainer removal
Fix various issues in macrocontainer removal

- also remove stopped containers
- skip already removed containers
- remove any Docker defined volumes
- actually remove storage subdirectories
