瀏覽代碼

add tasks for creating swapfiles. move tasks from common/main to common/basics.

Thomas Buck 2 年之前
父節點
當前提交
630e548fe9
共有 4 個文件被更改,包括 135 次插入86 次删除
  1. 7
    0
      roles/common/defaults/main.yml
  2. 87
    0
      roles/common/tasks/basics.yml
  3. 2
    86
      roles/common/tasks/main.yml
  4. 39
    0
      roles/common/tasks/swap.yml

+ 7
- 0
roles/common/defaults/main.yml 查看文件

@@ -23,3 +23,10 @@ ntp_servers:
23 23
   - 1.pool.ntp.org
24 24
   - 2.pool.ntp.org
25 25
   - 3.pool.ntp.org
26
+
27
+# swap, size in mb, times count. defaults to 1GB.
28
+# using multiple swapfiles gives some flexibility
29
+# about resizing/adding/removing them in future.
30
+swap_file_size: 1024
31
+swap_file_count: 1
32
+swap_file_path: '/swapfile'

+ 87
- 0
roles/common/tasks/basics.yml 查看文件

@@ -0,0 +1,87 @@
1
+---
2
+
3
+- name: Set hostname
4
+  hostname: name="{{ server_hostname }}"
5
+
6
+- name: Replace /etc/hosts
7
+  template: src=etc_hosts.j2 dest=/etc/hosts
8
+
9
+- name: Update apt cache
10
+  apt: update_cache=yes
11
+  tags:
12
+    - dependencies
13
+
14
+- name: Upgrade all safe packages
15
+  apt: upgrade=safe
16
+  tags:
17
+    - dependencies
18
+
19
+- name: Install necessities and nice-to-haves
20
+  apt:
21
+    name: "{{ packages }}"
22
+    state: present
23
+  vars:
24
+    packages:
25
+    - acl
26
+    - apache2
27
+    - apt-listchanges
28
+    - apt-transport-https
29
+    - apticron
30
+    - build-essential
31
+    - debian-goodies
32
+    - git
33
+    - gnupg
34
+    - htop
35
+    - iftop
36
+    - iotop
37
+    - molly-guard
38
+    - mosh
39
+    - php
40
+    - php-pgsql
41
+    - php-gd
42
+    - python3-software-properties
43
+    - ruby
44
+    - screen
45
+    - sudo
46
+    - tmux
47
+    - unattended-upgrades
48
+    - vim
49
+    - zsh
50
+  tags:
51
+    - dependencies
52
+
53
+- name: timezone - configure /etc/timezone
54
+  copy:
55
+    content: "{{ common_timezone | regex_replace('$', '\n') }}"
56
+    dest: /etc/timezone
57
+    owner: root
58
+    group: root
59
+    mode: 0644
60
+  register: common_timezone_config
61
+
62
+- name: timezone - Set localtime to UTC
63
+  file: src=/usr/share/zoneinfo/Etc/UTC dest=/etc/localtime
64
+  when: common_timezone_config.changed
65
+
66
+- name: timezone - reconfigure tzdata
67
+  command: dpkg-reconfigure --frontend noninteractive tzdata
68
+  when: common_timezone_config.changed
69
+
70
+- name: Apticron email configuration
71
+  template: src=apticron.conf.j2 dest=/etc/apticron/apticron.conf
72
+
73
+- name: Create data directory
74
+  file: state=directory path=/data
75
+
76
+- name: Set data directory permissions
77
+  file: state=directory path=/data group=mail mode=0775
78
+
79
+- name: Ensure US English locale exists
80
+  locale_gen:
81
+    name: en_US.UTF-8
82
+    state: present
83
+
84
+- name: Ensure German locale exists
85
+  locale_gen:
86
+    name: de_DE.UTF-8
87
+    state: present

+ 2
- 86
roles/common/tasks/main.yml 查看文件

@@ -1,94 +1,10 @@
1 1
 ---
2 2
 
3
-- name: Set hostname
4
-  hostname: name="{{ server_hostname }}"
5
-
6
-- name: Replace /etc/hosts
7
-  template: src=etc_hosts.j2 dest=/etc/hosts
8
-
9
-- name: Update apt cache
10
-  apt: update_cache=yes
11
-  tags:
12
-    - dependencies
13
-
14
-- name: Upgrade all safe packages
15
-  apt: upgrade=safe
16
-  tags:
17
-    - dependencies
18
-
19
-- name: Install necessities and nice-to-haves
20
-  apt:
21
-    name: "{{ packages }}"
22
-    state: present
23
-  vars:
24
-    packages:
25
-    - acl
26
-    - apache2
27
-    - apt-listchanges
28
-    - apt-transport-https
29
-    - apticron
30
-    - build-essential
31
-    - debian-goodies
32
-    - git
33
-    - gnupg
34
-    - htop
35
-    - iftop
36
-    - iotop
37
-    - molly-guard
38
-    - mosh
39
-    - php
40
-    - php-pgsql
41
-    - php-gd
42
-    - python3-software-properties
43
-    - ruby
44
-    - screen
45
-    - sudo
46
-    - tmux
47
-    - unattended-upgrades
48
-    - vim
49
-    - zsh
50
-  tags:
51
-    - dependencies
52
-
53
-- name: timezone - configure /etc/timezone
54
-  copy:
55
-    content: "{{ common_timezone | regex_replace('$', '\n') }}"
56
-    dest: /etc/timezone
57
-    owner: root
58
-    group: root
59
-    mode: 0644
60
-  register: common_timezone_config
61
-
62
-- name: timezone - Set localtime to UTC
63
-  file: src=/usr/share/zoneinfo/Etc/UTC dest=/etc/localtime
64
-  when: common_timezone_config.changed
65
-
66
-- name: timezone - reconfigure tzdata
67
-  command: dpkg-reconfigure --frontend noninteractive tzdata
68
-  when: common_timezone_config.changed
69
-
70
-- name: Apticron email configuration
71
-  template: src=apticron.conf.j2 dest=/etc/apticron/apticron.conf
72
-
73
-- name: Create data directory
74
-  file: state=directory path=/data
75
-
76
-- name: Set data directory permissions
77
-  file: state=directory path=/data group=mail mode=0775
78
-
79
-- name: Ensure US English locale exists
80
-  locale_gen:
81
-    name: en_US.UTF-8
82
-    state: present
83
-
84
-- name: Ensure German locale exists
85
-  locale_gen:
86
-    name: de_DE.UTF-8
87
-    state: present
88
-
3
+- include: basics.yml tags=basics
89 4
 - include: users.yml tags=users
90 5
 - include: apache.yml tags=apache
91 6
 - include: ufw.yml tags=ufw
92 7
 - include: security.yml tags=security
93 8
 - include: ntp.yml tags=ntp
94 9
 - include: postgres.yml
10
+- include: swap.yml

+ 39
- 0
roles/common/tasks/swap.yml 查看文件

@@ -0,0 +1,39 @@
1
+---
2
+# adapted from https://stackoverflow.com/a/24765946
3
+
4
+- name: Create swap files
5
+  command: dd if=/dev/zero of={{ '%s%d' | format(swap_file_path, item) }} bs=1024 count={{ swap_file_size }}K
6
+           creates="{{ '%s%d' | format(swap_file_path, item) }}"
7
+  loop: "{{ range(1, swap_file_count + 1) }}"
8
+
9
+- name: Change swap file permissions
10
+  file: path="{{ '%s%d' | format(swap_file_path, item) }}"
11
+        owner=root
12
+        group=root
13
+        mode=0600
14
+  loop: "{{ range(1, swap_file_count + 1) }}"
15
+
16
+- name: Check swap file types
17
+  command: file {{ '%s%d' | format(swap_file_path, item) }}
18
+  register: swapfile
19
+  loop: "{{ range(1, swap_file_count + 1) }}"
20
+
21
+- name: Make swap files
22
+  command: "mkswap {{ '%s%d' | format(swap_file_path, item) }}"
23
+  become: true
24
+  when: swapfile.results[item - 1].stdout.find('swap file') == -1
25
+  loop: "{{ range(1, swap_file_count + 1) }}"
26
+
27
+- name: Write swap entries in fstab
28
+  mount: name=none
29
+         src={{ '%s%d' | format(swap_file_path, item) }}
30
+         fstype=swap
31
+         opts=sw
32
+         passno=0
33
+         dump=0
34
+         state=present
35
+  loop: "{{ range(1, swap_file_count + 1) }}"
36
+
37
+- name: Mount swap
38
+  command: "swapon --all"
39
+  become: true

Loading…
取消
儲存