diff --git a/playbooks/bootstrap/roles/swap/defaults/main.yaml b/playbooks/bootstrap/roles/swap/defaults/main.yaml new file mode 100644 index 0000000..912d73f --- /dev/null +++ b/playbooks/bootstrap/roles/swap/defaults/main.yaml @@ -0,0 +1,20 @@ +# Copyright 2019 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +--- +swap_file_group: root +swap_file_mode: 0600 +swap_file_owner: root +swap_file_path: /root/swapfile + +swap_size_mb: "{{ ansible_memtotal_mb * 2 }}" diff --git a/playbooks/bootstrap/roles/swap/tasks/main.yaml b/playbooks/bootstrap/roles/swap/tasks/main.yaml new file mode 100644 index 0000000..5eb0fd3 --- /dev/null +++ b/playbooks/bootstrap/roles/swap/tasks/main.yaml @@ -0,0 +1,54 @@ +# Copyright 2019 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +--- +- name: Create swapfile + become: true + command: "fallocate -l {{ swap_size_mb }}M {{ swap_file_path }}" + args: + creates: "{{ swap_file_path }}" + +- name: Ensure swapfile permissions + become: true + file: + group: "{{ swap_file_group }}" + mode: "{{ swap_file_mode }}" + owner: "{{ swap_file_owner }}" + path: "{{ swap_file_path }}" + +- name: Register swap status + shell: "swapon -s | grep {{ swap_file_path }}" + changed_when: false + failed_when: _swap_status.rc > 1 + register: _swap_status + +- name: Format swapfile + become: true + command: "mkswap {{ swap_file_path }}" + when: not _swap_status.stdout + +- name: Add swapfile to fstab + become: true + mount: + path: swap + src: "{{ swap_file_path }}" + fstype: swap + opts: defaults + passno: 0 + dump: 0 + state: present + +- name: Enable swapfile + become: true + command: swapon -a + changed_when: false diff --git a/playbooks/bootstrap/site.yaml b/playbooks/bootstrap/site.yaml index dac8cf1..653f568 100644 --- a/playbooks/bootstrap/site.yaml +++ b/playbooks/bootstrap/site.yaml @@ -19,6 +19,10 @@ include_role: name: users + - name: Setup swap role + include_role: + name: swap + - name: Setup openstack.virtualenv role include_role: name: openstack.virtualenv diff --git a/tox.ini b/tox.ini index 8c0861c..eea61d0 100644 --- a/tox.ini +++ b/tox.ini @@ -13,7 +13,7 @@ whitelist_externals = bash commands = yamllint -s . flake8 - bash -c "ansible-lint -x 405 playbooks/*/*.yaml" + bash -c "ansible-lint -x 405 -x 306 playbooks/*/*.yaml" [testenv:venv] basepython = python3