Ian Wienand 1d01c6d3da Add configure-swap role
This is a role to configure swap and /opt on an infra host.

Originally we wrote this for devstack-gate with
Icd4e6d8ab84471ad06e69c3e0f9bac92776efc78.  It was ported into the
native devstack job with Iffe54fbccbccd68db08f79a1b51dd7f76dbff408.

This is really generic, and it's quite possible non-devstack jobs
would like to setup swap for jobs too, so o-z-j is a better home for
the role.

Change-Id: I0e9c846ace7fac8a1340746c6818fba6ec963018
2017-10-20 14:13:33 +11:00

66 lines
1.4 KiB
YAML

---
# If no ephemeral devices are available, use root filesystem
- name: Calculate required swap
set_fact:
swap_required: "{{ configure_swap_size - ansible_memory_mb['swap']['total'] | int }}"
- block:
- name: Get root filesystem
shell: df --output='fstype' /root | tail -1
register: root_fs
- name: Save root filesystem
set_fact:
root_filesystem: "{{ root_fs.stdout }}"
- debug: var=root_filesystem
# Note, we don't use a sparse device to avoid wedging when disk space
# and memory are both unavailable.
# Cannot fallocate on filesystems like XFS, so use slower dd
- name: Create swap backing file for non-EXT fs
when: '"ext" not in root_filesystem'
become: yes
command: dd if=/dev/zero of=/root/swapfile bs=1M count={{ swap_required }}
args:
creates: /root/swapfile
- name: Create sparse swap backing file for EXT fs
when: '"ext" in root_filesystem'
become: yes
command: fallocate -l {{ swap_required }}M /root/swapfile
args:
creates: /root/swapfile
- name: Ensure swapfile perms
become: yes
file:
path: /root/swapfile
owner: root
group: root
mode: 0600
- name: Make swapfile
become: yes
command: mkswap /root/swapfile
- name: Write swap to fstab
become: yes
mount:
path: none
src: /root/swapfile
fstype: swap
opts: sw
passno: 0
dump: 0
state: present
- name: Add all swap
become: yes
command: swapon -a
- debug: var=swap_required