18
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

Organization

Ansibleでswap領域を設定する

CentOS 7で動作確認しています。

  • 物理メモリと同等のスワップ領域を設定する
  • 何回実行しても大丈夫

という特長があります。

---
- set_fact:
    swap_file_path: /swapfile
- command: dd if=/dev/zero of={{ swap_file_path }} bs={{ ansible_memtotal_mb }} count=1M
  args:
    creates: '{{ swap_file_path }}'
- file:
    path: '{{ swap_file_path }}'
    mode: '0600'
- command: file {{ swap_file_path }}
  register: swap_file_test
- command: mkswap {{ swap_file_path }}
  when: swap_file_test.stdout.find('swap file') == -1
- command: swapon {{ swap_file_path }}
  when: ansible_swaptotal_mb < 1
- mount:
    name: swap
    src: '{{ swap_file_path }}'
    fstype: swap
    opts: defaults
    passno: '0'
    dump: '0'
    state: present

物理メモリの2倍にしたかったら、 {{ ansible_memtotal_mb }} のところを {{ ansible_memtotal_mb * 2 }} とかにすればいいと思います。

参考文献

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
18
Help us understand the problem. What are the problem?