- name: Configure swap file for PostgreSQL node hosts: dev become: true vars: swap_file_path: /swapfile swap_file_size_mb: 4096 # 4 GB tasks: - name: Check if swap file already exists stat: path: "{{ swap_file_path }}" register: swap_file_check - name: Create swap file command: dd if=/dev/zero of={{ swap_file_path }} bs=1M count={{ swap_file_size_mb }} when: not swap_file_check.stat.exists register: swap_created - name: Set correct permissions on swap file file: path: "{{ swap_file_path }}" owner: root group: root mode: '0600' when: swap_created is changed - name: Format swap file command: mkswap {{ swap_file_path }} when: swap_created is changed register: swap_formatted - name: Enable swap file command: swapon {{ swap_file_path }} when: swap_formatted is changed - name: Check if swap is already in fstab lineinfile: path: /etc/fstab regexp: '^{{ swap_file_path }}' state: absent check_mode: true register: fstab_check changed_when: false - name: Add swap to fstab for persistence lineinfile: path: /etc/fstab line: '{{ swap_file_path }} none swap sw 0 0' state: present when: fstab_check.found == 0 - name: Verify swap is active command: swapon --show register: swap_status changed_when: false - name: Display swap status debug: var: swap_status.stdout_lines