使用ansible添加交换内存 [英] Add swap memory with ansible
问题描述
我正在一个项目中,需要在服务器上具有交换内存,以避免某些长时间运行的python进程内存不足,并且第一次意识到我的ubuntu流浪者盒子和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屋!