From 19be5b46cbe7f59d64a99f5e724ed98cdd762a49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Such=C3=BD?= Date: Mon, 24 Feb 2020 19:01:38 +0100 Subject: [PATCH] add task to create swap This is mostly useful in AWS where swap does not exist and must be created manualy. --- tasks/swap.yml | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tasks/swap.yml diff --git a/tasks/swap.yml b/tasks/swap.yml new file mode 100644 index 0000000000..bfa9bacb06 --- /dev/null +++ b/tasks/swap.yml @@ -0,0 +1,56 @@ +# use variables: +# swap_file_size_mb +# swap_file_path + +- name: Create swap file + command: + cmd: "dd if=/dev/zero of={{ swap_file_path }} bs=1024 count={{ swap_file_size_mb }}k" + creates: "{{ swap_file_path }}" + tags: + - swap.file.create + - swap + + +- name: Change swap file permissions + file: path="{{ swap_file_path }}" + owner=root + group=root + mode=0600 + tags: + - swap.file.permissions + - swap + +- name: Check swap file type + command: file {{ swap_file_path }} + register: swapfile + tags: + - swap.file.mkswap + - swap + +- name: Make swap file + filesystem: + fstype: swap + dev: "{{ swap_file_path }}" + when: swapfile.stdout.find('swap file') == -1 + tags: + - swap.file.mkswap + - swap + +- name: Write swap entry in fstab + mount: path=none + src={{ swap_file_path }} + fstype=swap + opts=sw + passno=0 + dump=0 + state=present + tags: + - swap.fstab + - swap + +- name: Mount swap + command: "swapon {{ swap_file_path }}" + when: ansible_swaptotal_mb < 1 + tags: + - swap.file.swapon + - swap