-
-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from vitabaks/dev
add role "swap" - configure swap space (if not already exists)
- Loading branch information
Showing
7 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
# yamllint disable rule:line-length | ||
|
||
- name: Ensure swap exists | ||
command: swapon --show=SIZE --bytes --noheadings | ||
register: swap_exists | ||
changed_when: false | ||
when: swap_file_create|bool | ||
tags: swap, swap_create, swap_remove | ||
|
||
- name: Swap exists | ||
debug: | ||
msg: "swap_size_mb: {{ (swap_exists.stdout_lines|map('trim')|map('int')|sum / 1024 / 1024)|round|int }}" | ||
when: swap_exists.stdout is defined and swap_exists.stdout | length > 1 | ||
tags: swap, swap_create, swap_remove | ||
|
||
# if the swap exists and the size is not equal to swap_file_size_mb | ||
- block: | ||
- name: Disable all existing swaps | ||
command: swapoff --all | ||
|
||
- name: Remove swap from /etc/fstab | ||
lineinfile: | ||
path: /etc/fstab | ||
state: absent | ||
regexp: ' swap ' | ||
|
||
- name: Remove swap file (if exists) | ||
file: | ||
path: "{{ swap_file_path }}" | ||
state: absent | ||
when: (swap_exists.stdout is defined and swap_exists.stdout | length > 1) and | ||
((swap_exists.stdout_lines|map('trim')|map('int')|sum / 1024 / 1024)|round|int != swap_file_size_mb|int) | ||
tags: swap, swap_remove | ||
|
||
# if the swap does not exist | ||
- block: | ||
- name: Create swap file | ||
command: > | ||
dd if=/dev/zero of={{ swap_file_path }} bs=1M count={{ swap_file_size_mb }} | ||
creates='{{ swap_file_path }}' | ||
- name: Set permissions on swap file | ||
file: | ||
path: "{{ swap_file_path }}" | ||
owner: root | ||
group: root | ||
mode: 0600 | ||
|
||
- name: Make swap file if necessary | ||
command: mkswap {{ swap_file_path }} | ||
register: mkswap_result | ||
|
||
- name: Run swapon on the swap file | ||
command: swapon {{ swap_file_path }} | ||
|
||
- name: Manage swap file entry in fstab | ||
mount: | ||
name: none | ||
src: "{{ swap_file_path }}" | ||
fstype: swap | ||
opts: sw | ||
state: present | ||
when: (swap_exists.stdout is defined and swap_exists.stdout | length < 1) or | ||
(swap_exists.stdout_lines is defined and (swap_exists.stdout_lines|map('trim')|map('int')|sum / 1024 / 1024)|round|int != swap_file_size_mb|int) | ||
tags: swap, swap_create | ||
|
||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters