LogNotes

2025-07-23 08:13:44
#ok
- name: Создать и включить swap-файл 1ГБ
  hosts: all
  become: yes
  tasks:

    - name: Создаем swap-файл 1ГБ, если он не существует
      command: fallocate -l 1G /swapfile
      args:
        creates: /swapfile
      register: swapfile_created

    - name: Устанавливаем права 600 на /swapfile
      file:
        path: /swapfile
        owner: root
        group: root
        mode: '0600'

    - name: Проверяем активные swap-устройства
      command: swapon --noheadings --raw --show=NAME
      register: swapon_list
      changed_when: false

    - name: Инициализируем swap-файл /swapfile, если не инициализирован
      command: mkswap /swapfile
      when: "'/swapfile' not in swapon_list.stdout"
      
    - name: Активируем swap-файл /swapfile, если не активен
      command: swapon /swapfile
      when: "'/swapfile' not in swapon_list.stdout"

    - name: Добавляем swap в /etc/fstab
      mount:
        name: none
        src: /swapfile
        fstype: swap
        opts: sw
        state: present



====================
Если на целевой системе нет fallocate, можно заменить создание файла на dd:

text
- name: Создаем swap-файл 1ГБ (альтернатива dd)
  command: dd if=/dev/zero of=/swapfile bs=1M count=1024
  args:
    creates: /swapfile
← Previous Next →
Back to list