Skip to content

Commit 8841f75

Browse files
committed
Swap file. Lint.
1 parent f6a7bbb commit 8841f75

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

.yamllint

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
---
22
extends: default
33
rules:
4+
braces:
5+
max-spaces-inside: 1
6+
comments-indentation: false
7+
comments:
8+
min-spaces-from-content: 1
9+
octal-values:
10+
forbid-explicit-octal: true
11+
forbid-implicit-octal: true
412
line-length:
513
max: 400
614
level: warning

defaults/main.yml

+6
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,9 @@ elasticsearch_heap_size_max: 2g
482482
# used by Django, not necessarily by elasticsearch itself:
483483
elasticsearch_index_name: mailman
484484
elasticsearch_url: "http://127.0.0.1:9200/"
485+
486+
swap_configure: true # manage the swap or not
487+
swap_enable: true
488+
swap_file_path: /swapfile
489+
swap_file_size_mb: 4000
490+
swappiness: 1

tasks/elasticsearch.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
dest: "{{ item }}"
2929
owner: root
3030
group: elasticsearch
31-
mode: 0660
31+
mode: '0660'
3232
with_items:
3333
- /etc/elasticsearch/elasticsearch.yml
3434
- /etc/elasticsearch/jvm.options.d/heap.options

tasks/main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
- name: Include swap configuration
3+
ansible.builtin.include_tasks: swap.yml
24

35
- name: Include server configuration tasks
46
ansible.builtin.include_tasks: server.yml

tasks/swap.yml

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
# From https://stackoverflow.com/questions/24765930/add-swap-memory-with-ansible
3+
- name: Configure swap
4+
when: swap_configure | bool | default(false)
5+
block:
6+
7+
- name: Check if swap file exists
8+
ansible.builtin.stat:
9+
path: "{{ swap_file_path }}"
10+
get_checksum: false
11+
get_md5: false
12+
get_mime: false
13+
get_attributes: false
14+
register: swap_file_check
15+
changed_when: false
16+
17+
- name: Check if swap is on
18+
ansible.builtin.shell:
19+
set -o pipefail;
20+
swapon --show | grep {{ swap_file_path }}
21+
args:
22+
executable: /bin/bash
23+
register: swap_enabled
24+
changed_when: false
25+
failed_when: false
26+
27+
- name: Disable swap
28+
ansible.builtin.command: swapoff {{ swap_file_path }}
29+
register: swap_disabled
30+
changed_when: true
31+
when: >
32+
swap_file_check.stat.exists
33+
and swap_enabled.rc == 0
34+
and (not swap_enable
35+
or (swap_enable and swap_file_check.stat.size != (swap_file_size_mb * 1024 * 1024)))
36+
37+
- name: Delete the swap file
38+
ansible.builtin.file:
39+
path: "{{ swap_file_path }}"
40+
state: absent
41+
when: not swap_enable
42+
43+
- name: Configure swap
44+
when: swap_enable|bool
45+
block:
46+
47+
- name: Create or change the size of swap file
48+
ansible.builtin.command: dd if=/dev/zero of={{ swap_file_path }} count={{ swap_file_size_mb }} bs=1MiB
49+
register: swap_file_created
50+
changed_when: true
51+
when: >
52+
not swap_file_check.stat.exists
53+
or swap_file_check.stat.size != (swap_file_size_mb * 1024 * 1024)
54+
55+
- name: Change swap file permissions
56+
ansible.builtin.file:
57+
path: "{{ swap_file_path }}"
58+
owner: root
59+
group: root
60+
mode: '0600'
61+
62+
- name: Check if swap is formatted
63+
ansible.builtin.shell:
64+
set -o pipefail;
65+
file {{ swap_file_path }} | grep 'swap file'
66+
args:
67+
executable: /bin/bash
68+
register: swap_file_is_formatted
69+
changed_when: false
70+
failed_when: false
71+
72+
- name: Debugger1
73+
ansible.builtin.debug:
74+
var: swap_file_is_formatted
75+
76+
- name: Debugger2
77+
ansible.builtin.debug:
78+
var: swap_file_created
79+
80+
- name: Format swap file if it's not formatted
81+
ansible.builtin.command: mkswap {{ swap_file_path }}
82+
when: swap_file_is_formatted.rc > 0 or swap_file_created.changed
83+
changed_when: true
84+
85+
- name: Add swap entry to fstab
86+
ansible.posix.mount:
87+
path: none
88+
src: "{{ swap_file_path }}"
89+
fstype: swap
90+
opts: defaults
91+
passno: 0
92+
dump: 0
93+
state: present
94+
95+
- name: Turn on swap
96+
ansible.builtin.command: swapon -a
97+
when: swap_enable
98+
changed_when: false
99+
# original logic from article. However, let's start it every time.
100+
# when: swap_enabled.rc != 0 and swap_disabled.changed
101+
102+
- name: Configure swappiness
103+
ansible.posix.sysctl:
104+
name: vm.swappiness
105+
value: "{{ swappiness }}"
106+
state: present

0 commit comments

Comments
 (0)