使用ansible添加交换内存

gon*_*onz 25 memory swap amazon-web-services ansible

我正在开发一个项目,在我的服务器上交换内存需要避免一些python长时间运行的进程内存不足并且第一次意识到我的ubuntu流浪盒和AWS ubuntu实例还没有建立.

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

您将如何以幂等方式使用ansible设置基于文件的交换内存?ansible提供哪些模块或变量来帮助完成此设置(如ansible_swaptotal_mb变量)?

gon*_*onz 33

这是我目前的解决方案:

- 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
Run Code Online (Sandbox Code Playgroud)

  • 第一个任务中的`creates ="{{swap_file_path}}"`prop应该处理这个问题,如果文件存在`dd`就不会运行.我仍然在生产中使用它,当前的任务条件(当/创建时)似乎正在完成它们的工作,这是为了使这个幂等...你能详细说明这可能使文件归零的情况吗? (5认同)
  • gonz:你是对的; `creates`应该照顾它.我最初没有注意到. (3认同)
  • 我使用上面的内容并注意到交换文件没有停在我指定的文件大小,它继续运行并填充整个驱动器(在我的情况下为20GB).原来是伯爵旁边的`k`.使用Centos 7.5,它就像魅力一样 (2认同)

Ash*_*Ash 22

我尝试了上面的答案,但"Check swap file type"总是回来changed,因此不是幂等的,在编写Ansible任务时鼓励它作为最佳实践.

以下角色已在Ubuntu 14.04 Trusty上测试过,无需gather_facts启用.

- name: Set swap_file variable
  set_fact:
    swap_file: "{{swap_file_path}}"
  tags:
    - swap.set.file.path

- name: Check if swap file exists
  stat:
    path: "{{swap_file}}"
  register: swap_file_check
  tags:
    - swap.file.check

- name: Create swap file
  command: fallocate -l {{swap_file_size}} {{swap_file}}
  when: not swap_file_check.stat.exists
  tags:
    - swap.file.create

- name: Change swap file permissions
  file: path="{{swap_file}}"
        owner=root
        group=root
        mode=0600
  tags:
    - swap.file.permissions

- name: Format swap file
  sudo: yes
  command: "mkswap {{swap_file}}"
  when: not swap_file_check.stat.exists
  tags:
    - swap.file.mkswap

- name: Write swap entry in fstab
  mount: name=none
         src={{swap_file}}
         fstype=swap
         opts=sw
         passno=0
         dump=0
         state=present
  tags:
    - swap.fstab

- name: Turn on swap
  sudo: yes
  command: swapon -a
  when: not swap_file_check.stat.exists
  tags:
    - swap.turn.on

- name: Set swappiness
  sudo: yes
  sysctl:
    name: vm.swappiness
    value: "{{swappiness}}"
  tags:
    - swap.set.swappiness
Run Code Online (Sandbox Code Playgroud)

需要的Vars:

swap_file_path: /swapfile
# Use any of the following suffixes
# c=1
# w=2
# b=512
# kB=1000
# K=1024
# MB=1000*1000
# M=1024*1024
# xM=M
# GB=1000*1000*1000
# G=1024*1024*1024
swap_file_size: 4G
swappiness: 1
Run Code Online (Sandbox Code Playgroud)

  • 从 Ansible 2.9 开始,“sudo”不再是有效的关键字类型。因此,“sudo: yes”应该更改为“become: yes”。 (2认同)

Gre*_*cki 12

我基于伟大的Ashley 的回答(请点赞!)并添加了以下额外功能:

  • 管理交换与否的全局标志,
  • 允许更改交换大小,
  • 允许禁用交换,

...加上这些技术改进:

  • 全幂等(仅okskipping当什么也没发生改变,否则一些changed),
  • 使用dd而不是fallocate为了与 XFS fs 兼容(有关更多信息,请参阅此答案),

在 Centos 7.7 和 Ansible 2.9.10 上测试。

参数:

swap_configure: true # manage the swap or not 
swap_enable: true
swap_file_path: /swapfile
swap_file_size_mb: 4096
swappiness: 1
Run Code Online (Sandbox Code Playgroud)

编码:

- name: Configure swap
  when: swap_configure | bool | default(false)
  block:

    - name: Check if swap file exists
      stat:
        path: "{{swap_file_path}}"
      register: swap_file_check
      changed_when: false

    - name: Check if swap is on
      shell: swapon --show | grep {{swap_file_path}}
      register: swap_enabled
      changed_when: false
      failed_when: false

    - name: Disable swap
      command: swapoff {{swap_file_path}}
      register: swap_disabled
      when: >
        swap_file_check.stat.exists
        and swap_enabled.rc == 0
        and (not swap_enable
             or (swap_enable and swap_file_check.stat.size != (swap_file_size_mb * 1024 * 1024)))

    - name: Delete the swap file
      file:
        path: "{{swap_file_path}}"
        state: absent
      when: not swap_enable

    - name: Configure swap
      when: swap_enable|bool
      block:

        - name: Create or change the size of swap file
          command: dd if=/dev/zero of={{swap_file_path}} count={{swap_file_size_mb}} bs=1MiB
          register: swap_file_created
          when: >
            not swap_file_check.stat.exists
            or swap_file_check.stat.size != (swap_file_size_mb * 1024 * 1024)

        - name: Change swap file permissions
          file: path="{{swap_file_path}}"
            owner=root
            group=root
            mode=0600

        - name: Check if swap is formatted
          shell: file {{swap_file_path}} | grep 'swap file'
          register: swap_file_is_formatted
          changed_when: false
          failed_when: false

        - name: Format swap file if it's not formatted
          command: mkswap {{swap_file_path}}
          when: swap_file_is_formatted.rc > 0 or swap_file_created.changed

        - name: Add swap entry to fstab
          mount: name=none
            src={{swap_file_path}}
            fstype=swap
            opts=sw
            passno=0
            dump=0
            state=present

        - name: Turn on swap
          shell: swapon -a
          when: swap_enabled.rc != 0 and swap_disabled.changed

        - name: Configure swappiness
          sysctl:
            name: vm.swappiness
            value: "{{ swappiness }}"
            state: present
Run Code Online (Sandbox Code Playgroud)

  • 是的,有一个错误,“打开交换”需要检查“swap_enabled.rc!= 0”是否为0,因为只有当交换已经打开时它才会为0,在这种情况下不需要再次启用它。 (2认同)