Skip to content

Commit

Permalink
swap support
Browse files Browse the repository at this point in the history
  • Loading branch information
perryflynn committed Apr 17, 2024
1 parent 7420ddd commit 8d7431d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ a Dockerfile and some scripts to build a custom Arch Linux installer iso with ad

Run `perrys-ansible-apply.sh` as root.

It pull automatically pull the correct playbook from this repo by the hostname of the operating system.
It pulls automatically the correct playbook from this repo by the hostname of the operating system.

## TODO

- [x] Swap File
- [ ] Disk Encryption
7 changes: 7 additions & 0 deletions mydemoarch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

tasks:

- name: Install Swapfile
include_role:
name: swapfilesetup
vars:
swapsizegb: 8
swappath: /swapfile

- name: Install user account
block:

Expand Down
5 changes: 5 additions & 0 deletions roles/swapsetup/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
# roles/swapsetup/defaults/main.yml

swapsizegb: 4
swappath: /swapfile
50 changes: 50 additions & 0 deletions roles/swapsetup/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
# roles/swapsetup/tasks/main.yml

- name: Ensure parameters
assert:
that:
- ansible_distribution == 'Archlinux'

- name: Check if swapfile exists
shell:
executable: /usr/bin/bash
cmd: |
if ( swapon --noheadings --show=NAME,TYPE,SIZE,USED --bytes | grep -P "^/swapfile\s+" )
then
exit 98
fi
exit 99
changed_when: False
failed_when: swapfilestatus.rc not in [ 98, 99 ]
register: swapfilestatus

- name: Create swapfile
filesize:
path: "{{swappath}}"
size: "{{swapsizegb}}G"
owner: root
group: root
mode: u=rw,go=-
when: swapfilestatus.rc == 99
register: mkswapfile

- name: Format swapfile
filesystem:
dev: "{{swappath}}"
fstype: swap
when: mkswapfile.changed

- name: Ensure swapfile in fstab
lineinfile:
path: /etc/fstab
state: present
regexp: '^{{swappath}}\s+'
line: '{{swapfile}} none swap defaults 0 0'

- name: Mount swapfile
shell:
executable: /usr/bin/bash
cmd: |
swapon "{{swappath}}"
when: swapfilestatus.rc == 99 and mkswapfile.changed

0 comments on commit 8d7431d

Please sign in to comment.