使用ansible添加交换内存

2024-01-05

我正在开发一个项目,其中需要在我的服务器上拥有交换内存,以避免某些 python 长时间运行的进程耗尽内存,并第一次意识到我的 ubuntu vagrant box 和 AWS ubuntu 实例还没有交换内存设置。

In https://github.com/ansible/ansible/issues/5241 https://github.com/ansible/ansible/issues/5241讨论了可能的内置解决方案但从未实施,所以我猜这应该是一个非常常见的自动化任务。

如何使用 ansible 以幂等方式设置基于文件的交换内存? ansible 为这个设置提供了哪些模块或变量的帮助(例如ansible_swaptotal_mb多变的) ?


这是我当前的解决方案:

- 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用ansible添加交换内存 的相关文章

随机推荐