---
# swap role — 確保 swap 已設定（110 專用）
# 預期狀態: swap.img (3.8G) + swap2.img (4G) 共 ~8G

- name: "Swap | 確認 swap2.img 存在"
  ansible.builtin.stat:
    path: /swap2.img
  register: swap2_stat

- name: "Swap | 建立 swap2.img"
  ansible.builtin.command:
    cmd: "fallocate -l 4G /swap2.img"
    creates: /swap2.img
  when: not swap2_stat.stat.exists

- name: "Swap | 格式化 swap2.img"
  ansible.builtin.command:
    cmd: "mkswap /swap2.img"
  when: not swap2_stat.stat.exists

- name: "Swap | 設定 swap2.img 權限"
  ansible.builtin.file:
    path: /swap2.img
    mode: "0600"

- name: "Swap | 加入 swap2.img 到 fstab"
  ansible.builtin.lineinfile:
    path: /etc/fstab
    line: "/swap2.img none swap sw 0 0"
    state: present

- name: "Swap | 啟用 swap"
  ansible.builtin.command:
    cmd: "swapon --all"
  changed_when: false
