Skip to content

Commit 6ed2145

Browse files
committed
Added option to create swap, updated README, resolves #14.
1 parent 026a642 commit 6ed2145

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,38 @@ If you're testing with vagrant, you can use this command:
9393
ansible-playbook -i vagrant_ansible_inventory_default --private-key=~/.vagrant.d/insecure_private_key -v vagrant.yml
9494
```
9595

96+
## Advanced Options
97+
98+
### Creating a swap file
99+
100+
By default, the playbook won't create a swap file. To create/enable swap, simply change the values in `roles/base/vars/main.yml`.
101+
102+
You can also override these values in the main playbook, for example:
103+
104+
```
105+
---
106+
107+
- name: Create a {{ application_name }} virtual machine via vagrant
108+
hosts: all
109+
sudo: yes
110+
sudo_user: root
111+
remote_user: vagrant
112+
vars:
113+
- setup_git_repo: yes
114+
- update_apt_cache: yes
115+
vars_files:
116+
- env_vars/base.yml
117+
- env_vars/local.yml
118+
119+
roles:
120+
- { role: base, create_swap_file: yes, swap_file_size_kb: 1024 }
121+
- db
122+
- web
123+
- memcached
124+
```
125+
126+
This will create and mount a 1GB swap. Note that block size is 1024, so the size of the swap file will be 1024 x `swap_file_size_kb`.
127+
96128
## Useful Links
97129
- [Ansible - Getting Started](http://docs.ansible.com/intro_getting_started.html)
98130
- [Ansible - Best Practices](http://docs.ansible.com/playbooks_best_practices.html)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
- name: Create swap file
2+
command: dd if=/dev/zero of={{ swap_file_path }} bs=1024 count={{ swap_file_size_kb }}k
3+
creates="{{ swap_file_path }}"
4+
tags: swap.file.create
5+
6+
- name: Change swap file permissions
7+
file: path="{{ swap_file_path }}"
8+
owner=root
9+
group=root
10+
mode=0600
11+
tags: swap.file.permissions
12+
13+
- name: Check swap file type
14+
command: file {{ swap_file_path }}
15+
register: swapfile
16+
tags: swap.file.mkswap
17+
18+
- name: Make swap file
19+
command: "sudo mkswap {{ swap_file_path }}"
20+
when: swapfile.stdout.find('swap file') == -1
21+
tags: swap.file.mkswap
22+
23+
- name: Write swap entry in fstab
24+
mount: name=none
25+
src={{ swap_file_path }}
26+
fstype=swap
27+
opts=sw
28+
passno=0
29+
dump=0
30+
state=present
31+
tags: swap.fstab
32+
33+
- name: Mount swap
34+
command: "swapon {{ swap_file_path }}"
35+
when: ansible_swaptotal_mb < 1
36+
tags: swap.file.swapon

roles/base/tasks/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
---
22

3+
- include: create_swap_file.yml
4+
when: create_swap_file
5+
tags: swap
6+
37
- name: Ensure bash, OpenSSl, and libssl are the latest versions
48
apt: name={{ item }} update_cache={{ update_apt_cache }} state=latest
59
with_items:

roles/base/vars/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
3+
create_swap_file: no
4+
swap_file_path: /swapfile
5+
swap_file_size_kb: 512

0 commit comments

Comments
 (0)