From 6ed21453abd5b84b5bc7ef65bfedaec537988af8 Mon Sep 17 00:00:00 2001 From: jonathan Date: Wed, 4 Mar 2015 19:17:34 -0500 Subject: [PATCH] Added option to create swap, updated README, resolves #14. --- README.md | 32 ++++++++++++++++++++++++ roles/base/tasks/create_swap_file.yml | 36 +++++++++++++++++++++++++++ roles/base/tasks/main.yml | 4 +++ roles/base/vars/main.yml | 5 ++++ 4 files changed, 77 insertions(+) create mode 100644 roles/base/tasks/create_swap_file.yml create mode 100644 roles/base/vars/main.yml diff --git a/README.md b/README.md index 3a644534..5d6922ae 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/roles/base/tasks/create_swap_file.yml b/roles/base/tasks/create_swap_file.yml new file mode 100644 index 00000000..4bc93949 --- /dev/null +++ b/roles/base/tasks/create_swap_file.yml @@ -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 \ No newline at end of file diff --git a/roles/base/tasks/main.yml b/roles/base/tasks/main.yml index bc60a03f..b9472133 100644 --- a/roles/base/tasks/main.yml +++ b/roles/base/tasks/main.yml @@ -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: diff --git a/roles/base/vars/main.yml b/roles/base/vars/main.yml new file mode 100644 index 00000000..f773fd1e --- /dev/null +++ b/roles/base/vars/main.yml @@ -0,0 +1,5 @@ +--- + +create_swap_file: no +swap_file_path: /swapfile +swap_file_size_kb: 512 \ No newline at end of file