Skip to content

Commit

Permalink
Added option to create swap, updated README, resolves #14.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcalazan committed Mar 5, 2015
1 parent 026a642 commit 6ed2145
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,38 @@ If you're testing with vagrant, you can use this command:
ansible-playbook -i vagrant_ansible_inventory_default --private-key=~/.vagrant.d/insecure_private_key -v vagrant.yml
```

## Advanced Options

### Creating a swap file

By default, the playbook won't create a swap file. To create/enable swap, simply change the values in `roles/base/vars/main.yml`.

You can also override these values in the main playbook, for example:

```
---
- name: Create a {{ application_name }} virtual machine via vagrant
hosts: all
sudo: yes
sudo_user: root
remote_user: vagrant
vars:
- setup_git_repo: yes
- update_apt_cache: yes
vars_files:
- env_vars/base.yml
- env_vars/local.yml
roles:
- { role: base, create_swap_file: yes, swap_file_size_kb: 1024 }
- db
- web
- memcached
```

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`.

## Useful Links
- [Ansible - Getting Started](http://docs.ansible.com/intro_getting_started.html)
- [Ansible - Best Practices](http://docs.ansible.com/playbooks_best_practices.html)
Expand Down
36 changes: 36 additions & 0 deletions roles/base/tasks/create_swap_file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
- name: Create swap file
command: dd if=/dev/zero of={{ swap_file_path }} bs=1024 count={{ swap_file_size_kb }}k
creates="{{ swap_file_path }}"
tags: swap.file.create

- name: Change swap file permissions
file: path="{{ swap_file_path }}"
owner=root
group=root
mode=0600
tags: swap.file.permissions

- name: Check swap file type
command: file {{ swap_file_path }}
register: swapfile
tags: swap.file.mkswap

- name: Make swap file
command: "sudo mkswap {{ swap_file_path }}"
when: swapfile.stdout.find('swap file') == -1
tags: swap.file.mkswap

- name: Write swap entry in fstab
mount: name=none
src={{ swap_file_path }}
fstype=swap
opts=sw
passno=0
dump=0
state=present
tags: swap.fstab

- name: Mount swap
command: "swapon {{ swap_file_path }}"
when: ansible_swaptotal_mb < 1
tags: swap.file.swapon
4 changes: 4 additions & 0 deletions roles/base/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
---

- include: create_swap_file.yml
when: create_swap_file
tags: swap

- name: Ensure bash, OpenSSl, and libssl are the latest versions
apt: name={{ item }} update_cache={{ update_apt_cache }} state=latest
with_items:
Expand Down
5 changes: 5 additions & 0 deletions roles/base/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

create_swap_file: no
swap_file_path: /swapfile
swap_file_size_kb: 512

0 comments on commit 6ed2145

Please sign in to comment.