使用 ansible 添加交换内存 [英] Add swap memory with ansible
问题描述
我正在开发一个项目,需要在我的服务器上使用交换内存来避免一些 python 长时间运行的进程内存不足,并第一次意识到我的 ubuntu vagrant 盒子和 AWS ubuntu 实例没有已经设置好了.
I'm working on a project where having swap memory on my servers is a needed to avoid some python long running processes to go out of memory and realized for the first time that my ubuntu vagrant boxes and AWS ubuntu instances didn't already have one set up.
在 https://github.com/ansible/ansible/issues/5241 中,一个可能的内置解决方案已讨论但从未实施,所以我猜这应该是一项非常常见的自动化任务.
In https://github.com/ansible/ansible/issues/5241 a possible built in solution was discussed but never implemented, so I'm guessing this should be a pretty common task to automatize.
您将如何以幂等方式使用 ansible 设置基于文件的交换内存?ansible 为这个设置提供了哪些模块或变量(比如 ansible_swaptotal_mb
变量)?
How would you set up a file based swap memory with ansible in an idempotent way? What modules or variables does ansible provide help with this setup (like ansible_swaptotal_mb
variable) ?
推荐答案
这是我目前的解决方案:
This is my current solution:
- name: Create swap file
command: dd if=/dev/zero of={{ swap_file_path }} bs=1024 count={{ swap_file_size_mb }}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
这篇关于使用 ansible 添加交换内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!