cleanserver.yml 1.06 KB
---
- hosts: FormatAppDisk
  become: true
  tasks:
    # Module Delete Process That Open Port Except SSH
    - name: Get running processes
      shell: lsof -n -i4TCP |grep -v 'ssh' | grep LISTEN | awk '{ print $2 }'
      register: running_processes

    - name: Kill running processes
      shell: "kill {{ item }}"
      with_items: "{{ running_processes.stdout_lines }}"

    - wait_for:
        path: "/proc/{{ item }}/status"
        state: absent
      with_items: "{{ running_processes.stdout_lines }}"
      ignore_errors: yes
      register: killed_processes

    - name: Force kill stuck processes
      shell: "kill -9 {{ item }}"
      with_items: "{{ killed_processes.results | select('failed') | map(attribute='item') | list }}"
    # END
    
    # Read device information (always use unit when probing)
    - parted: device="{{ app_disk }}" unit=MiB
      register: sdb_info
# Remove all partitions from disk
    - parted:
        device: "{{ app_disk }}"
        number: "{{ item.num }}"
        state: absent
      with_items:
        - "{{ sdb_info.partitions }}"