--- # Create swap file on low-memory servers - name: Check if swap file exists ansible.builtin.stat: path: /swapfile register: swap_check - name: Create swap file when: not swap_check.stat.exists block: - name: Allocate swap file ansible.builtin.command: cmd: "dd if=/dev/zero of=/swapfile bs=1M count={{ swap_size_mb }}" changed_when: true - name: Set swap file permissions ansible.builtin.file: path: /swapfile mode: "0600" - name: Format swap file ansible.builtin.command: cmd: mkswap /swapfile changed_when: true - name: Enable swap file ansible.builtin.command: cmd: swapon /swapfile changed_when: true - name: Add swap to fstab ansible.builtin.lineinfile: path: /etc/fstab line: "/swapfile none swap sw 0 0" state: present