kayobe/ansible/roles/swap/tasks/main.yml
Mark Goddard 0c074431d0 Support configuration of swap
Supports creating and using swap files, or using pre-existing swap
devices.

Story: 2004958
Task: 29390

Change-Id: Iadb540f42036a4a63cdd5b695b82f1504b3a4a28
2022-12-07 14:23:18 +00:00

47 lines
1.1 KiB
YAML

---
- name: Validate swap configuration
ansible.utils.validate:
data: "{{ swap }}"
criteria: "{{ lookup('file', 'schema.json') }}"
# The following two tasks were adapted from
# https://github.com/geerlingguy/ansible-role-swap.
- name: Ensure swap file exists
command:
cmd: "{{ swap_file_create_command }}"
creates: "{{ item.path }}"
register: swap_file_create
loop: "{{ swap }}"
when: item.path is defined
- name: Set permissions on swap file
file:
path: "{{ item.path }}"
owner: root
group: root
mode: 0600
loop: "{{ swap }}"
when: item.path is defined
- name: Ensure swap filesystem is present
filesystem:
dev: "{{ item.device | default(item.path) }}"
fstype: "swap"
loop: "{{ swap }}"
- name: Ensure swap device is present in fstab
mount:
name: "none"
src: "{{ item.device | default(item.path) }}"
fstype: "swap"
state: "present"
loop: "{{ swap }}"
# It does no harm to run this when swap is already active.
- name: Enable swap
command: "/sbin/swapon -a"
when:
- ansible_facts.swaptotal_mb == 0
changed_when: true