swapfile/tasks/main.yaml
author Luke Hoersten <luke@hoersten.org>
Sat, 10 Feb 2024 15:41:05 -0600
changeset 230 07f08cc98da0
permissions -rw-r--r--
Added swapfile role.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
230
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     1
---
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     2
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     3
- name: check if swap file exists
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     4
  stat:
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     5
    path: "{{swapfile_path}}"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     6
  register: swapfile_check
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     7
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     8
- name: create swap file
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     9
  become: yes
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    10
  command: "fallocate -l {{swapfile_size}} {{swapfile_path}}"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    11
  when: not swapfile_check.stat.exists
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    12
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    13
- name: set swap file permissions
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    14
  become: yes
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    15
  file:
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    16
    path: "{{swapfile_path}}"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    17
    mode: "0600"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    18
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    19
- name: format swap file
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    20
  become: yes
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    21
  command: "mkswap {{swapfile_path}}"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    22
  when: not swapfile_check.stat.exists
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    23
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    24
- name: write swap entry in fstab
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    25
  become: yes
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    26
  mount:
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    27
    name: "none"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    28
    src: "{{swapfile_path}}"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    29
    fstype: "swap"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    30
    opts: "sw"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    31
    passno: 0
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    32
    dump: 0
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    33
    state: "present"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    34
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    35
- name: turn on swap
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    36
  become: yes
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    37
  command: "swapon {{swapfile_path}}"
07f08cc98da0 Added swapfile role.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    38
  when: not swapfile_check.stat.exists