Skip to content

Instantly share code, notes, and snippets.

@tokida
Last active May 14, 2019 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokida/2dfde38a73a873f4ba72f42281dbf7fe to your computer and use it in GitHub Desktop.
Save tokida/2dfde38a73a873f4ba72f42281dbf7fe to your computer and use it in GitHub Desktop.
create swap on Ubuntu
---
- 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
[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
swap_file: /swapfile
@bassbone
Copy link

突然のコメント失礼します。
細かい点で恐縮ですが、- name: carete swapfile- name: create swapfileが正しいのでは無いかと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment