From 4ec9a020a1cc4f416f2345b1424fd517171c4183 Mon Sep 17 00:00:00 2001 From: Erik Stambaugh Date: Sat, 20 Jan 2024 09:12:03 -0800 Subject: [PATCH] Add swap file to instance --- ansible/roles/common/defaults/main.yaml | 3 ++ ansible/roles/common/tasks/main.yaml | 39 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 ansible/roles/common/defaults/main.yaml diff --git a/ansible/roles/common/defaults/main.yaml b/ansible/roles/common/defaults/main.yaml new file mode 100644 index 0000000..6537893 --- /dev/null +++ b/ansible/roles/common/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +swap_file_path: "/swapfile" +swap_file_size_mb: '512' diff --git a/ansible/roles/common/tasks/main.yaml b/ansible/roles/common/tasks/main.yaml index dea1a3b..013dc4e 100644 --- a/ansible/roles/common/tasks/main.yaml +++ b/ansible/roles/common/tasks/main.yaml @@ -65,3 +65,42 @@ block: | alias ls='ls --color=auto' +## set up swap + +- name: create swap file + command: "dd if=/dev/zero of={{ swap_file_path }} bs=1024 count={{ swap_file_size_mb }}k" + args: + creates: "{{ swap_file_path }}" + +- name: swap file permissions + file: + path: "{{ swap_file_path }}" + owner: root + group: root + mode: "0600" + +- name: check swap file type + command: "file {{ swap_file_path }}" + register: swapfile + +- name: mkswap + command: "mkswap {{ swap_file_path }}" + when: swapfile.stdout.find('swap file') == -1 + +- name: swapfile in fstab + mount: + name: none + src: "{{ swap_file_path }}" + fstype: swap + opts: sw + passno: 0 + dump: 0 + state: present + +- name: mount swap + command: "swapon {{ swap_file_path }}" + when: ansible_swaptotal_mb < 1 + + + +