Cleanup_controller

These tasks will cleanup the templates, projects and workflows that are left behind
after deleting the organization that owned them.

---

- name: Controller | Read the job_template list
  ansible.builtin.uri:
    url: "https://{{ controller_name }}/api/controller/v2/job_templates/?organization=null"
    user: "{{ aap_username }}"
    password: "{{ aap_password }}"
    method: GET
    body_format: json
    force_basic_auth: true
    validate_certs: false
  register: _controller_job_templates

- name: Get the job_template list of dicts
  ansible.builtin.set_fact:
    _job_templates: "{{ _controller_job_templates.json.results }}"

- name: Controller | Read the workflow_template list
  ansible.builtin.uri:
    url: "https://{{ controller_name }}/api/controller/v2/workflow_job_templates/?organization=null"
    user: "{{ aap_username }}"
    password: "{{ aap_password }}"
    method: GET
    body_format: json
    force_basic_auth: true
    validate_certs: false
  register: _workflow_job_templates

- name: Get the workflow job_template list of dicts
  ansible.builtin.set_fact:
    _workflow_job_templates: "{{ _workflow_job_templates.json.results }}"

- name: Controller | Read the project list
  ansible.builtin.uri:
    url: "https://{{ controller_name }}/api/controller/v2/projects/?organization=null"
    user: "{{ aap_username }}"
    password: "{{ aap_password }}"
    method: GET
    body_format: json
    force_basic_auth: true
    validate_certs: false
  register: _controller_projects

- name: Get the projects list of dicts
  ansible.builtin.set_fact:
    _projects: "{{ _controller_projects.json.results }}"

- name: Remove stale workflow_job_templates
  ansible.builtin.uri:
    url: "https://{{ controller_name }}/api/controller/v2/workflow_job_templates/{{ workflow_del.id }}/"
    user: "{{ aap_username }}"
    password: "{{ aap_password }}"
    method: DELETE
    headers: {Content-Type: application/json}
    force_basic_auth: true
    validate_certs: false
    status_code: 204
  loop:
    "{{ _workflow_job_templates }}"
  loop_control:
    loop_var: workflow_del
  when: _workflow_job_templates | length > 0

- name: Remove stale job_templates
  ansible.builtin.uri:
    url: "https://{{ controller_name }}/api/controller/v2/job_templates/{{ template_del.id }}/"
    user: "{{ aap_username }}"
    password: "{{ aap_password }}"
    method: DELETE
    headers: {Content-Type: application/json}
    force_basic_auth: true
    validate_certs: false
    status_code: 204
  loop:
    "{{ _job_templates }}"
  loop_control:
    loop_var: template_del
  when: _job_templates | length > 0

- name: Remove stale projects
  ansible.builtin.uri:
    url: "https://{{ controller_name }}/api/controller/v2/projects/{{ project_del.id }}/"
    user: "{{ aap_username }}"
    password: "{{ aap_password }}"
    method: DELETE
    headers: {Content-Type: application/json}
    force_basic_auth: true
    validate_certs: false
    status_code: 204
  loop:
    "{{ _projects }}"
  loop_control:
    loop_var: project_del
  when: _projects | length > 0