Last active
May 14, 2019 02:55
-
-
Save tokida/2dfde38a73a873f4ba72f42281dbf7fe to your computer and use it in GitHub Desktop.
create swap on Ubuntu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: carete swapfile | |
command: dd if=/dev/zero of={{ swap_file }} bs={{ ansible_memtotal_mb }} count=1M | |
args: | |
creates: '{{ swap_file }}' | |
- file: | |
path: '{{ swap_file }}' | |
mode: '0600' | |
- command: file {{ swap_file }} | |
register: swap_file_test | |
- command: mkswap {{ swap_file }} | |
when: swap_file_test.stdout.find('swap file') == -1 | |
- name: copy swap config file | |
template: src=templates/swapfile.swap.j2 dest=/etc/systemd/system/swapfile.swap | |
- name: start swapfile.swap | |
systemd: | |
name: swapfile.swap | |
state: restarted | |
daemon_reload: yes | |
enabled: yes | |
become: yes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Turn on swap | |
[Service] | |
Type=oneshot | |
Environment="SWAPFILE={{ swap_file }}" | |
RemainAfterExit=true | |
ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE} | |
ExecStart=/usr/bin/sh -c "/sbin/swapon $(/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)" | |
ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)" | |
ExecStopPost=/usr/bin/sh -c "/sbin/losetup -d $(/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)" | |
[Install] | |
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
swap_file: /swapfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
突然のコメント失礼します。
細かい点で恐縮ですが、
- name: carete swapfile
は- name: create swapfile
が正しいのでは無いかと思います。