Add swap memory with ansible

前端 未结 5 2053
醉酒成梦
醉酒成梦 2021-02-05 01:57

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

5条回答
  •  花落未央
    2021-02-05 02:32

    I based my take on the great Ashley's answer (please upvote it!) and added the following extra features:

    • global flag to manage the swap or not,
    • allow changing the swap size,
    • allow disabling swap,

    ...plus these technical improvements:

    • full idempotency (only ok & skipping when nothing is changed, otherwise some changed),
    • use dd instead of fallocate for compatibility with XFS fs (see this answer for more info),

    Tested on Centos 7.7 with Ansible 2.9.10.

    Parameters:

    swap_configure: true # manage the swap or not 
    swap_enable: true
    swap_file_path: /swapfile
    swap_file_size_mb: 4096
    swappiness: 1
    

    The code:

    - 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
    

提交回复
热议问题