diff --git a/debian12-basic.yml b/debian12-basic.yml index 6cc9096..51af9d7 100644 --- a/debian12-basic.yml +++ b/debian12-basic.yml @@ -2,6 +2,9 @@ - name: Update web servers hosts: all remote_user: root + vars: + swapfile_size: 1 + swapfile_path: /swapfile tasks: - name: Update all packages to their latest version ansible.builtin.apt: @@ -12,4 +15,41 @@ ansible.builtin.apt: pkg: - htop - - unp \ No newline at end of file + - unp + + - name: Create swap file + command: | + {% if swapfile_fallocate %} + fallocate -l {{ ((swapfile_size) | int * 1024 * 1024) }} {{ swapfile_path }} + {% else %} + dd if=/dev/zero of={{ swapfile_path }} bs=1M count={{ swapfile_size }} + {% endif %} + args: + creates: "{{ swapfile_path }}" + register: swapfile_register_create + + - name: Set swap file permissions + file: + path: "{{ swapfile_path }}" + state: "file" + owner: "root" + group: "root" + mode: "0600" + + - name: Initialize swap file + command: mkswap {{ swapfile_path }} + when: swapfile_register_create is changed + + - name: Enable swap file + command: swapon {{ swapfile_path }} + when: swapfile_register_create is changed + + - name: Manage swap file in /etc/fstab + mount: + src: "{{ swapfile_path }}" + name: "none" + fstype: "swap" + opts: "sw,nofail" + dump: "0" + passno: "0" + state: "present" \ No newline at end of file