feat: enhance Ansible playbook with swap file management

- Update inventory to use root user with SSH options for security
- Add tasks for creating, configuring, and enabling a swap file
- Set swappiness parameter temporarily and permanently
- Ensure swap file is added to /etc/fstab for automatic mounting
- Include checks and debug information for swap status
This commit is contained in:
2025-09-16 15:29:40 +03:00
parent 0d5dc67eb9
commit a1586e78b3
2 changed files with 68 additions and 18 deletions

View File

@@ -29,10 +29,6 @@
run_once: true
ignore_errors: yes
- name: Переключиться на root для начальной настройки
set_fact:
ansible_user: root
- name: Обновить кэш пакетов
apt:
update_cache: yes
@@ -58,6 +54,60 @@
- prometheus-node-exporter
state: present
- name: Проверить существование swap-файла
stat:
path: /swapfile
register: swap_file_stat
- name: Создать swap-файл (2GB)
command: fallocate -l 2G /swapfile
when: not swap_file_stat.stat.exists
- name: Установить правильные права на swap-файл
file:
path: /swapfile
mode: '0600'
owner: root
group: root
- name: Настроить swap-файл
command: mkswap /swapfile
when: not swap_file_stat.stat.exists
- name: Включить swap-файл
command: swapon /swapfile
when: not swap_file_stat.stat.exists
- name: Настроить swappiness = 10 (временно)
sysctl:
name: vm.swappiness
value: '10'
state: present
reload: yes
- name: Настроить swappiness = 10 (постоянно)
lineinfile:
path: /etc/sysctl.conf
regexp: '^vm\.swappiness\s*='
line: 'vm.swappiness = 10'
state: present
- name: Добавить swap-файл в /etc/fstab для автоматического монтирования
lineinfile:
path: /etc/fstab
line: '/swapfile none swap sw 0 0'
state: present
create: yes
- name: Проверить статус swap
command: swapon --show
register: swap_status
changed_when: false
- name: Показать информацию о swap
debug:
var: swap_status.stdout_lines
- name: Включить и запустить prometheus-node-exporter
systemd:
name: prometheus-node-exporter
@@ -129,9 +179,6 @@
register: user_exists
failed_when: false
- name: Переключиться на пользователя deploy
meta: reset_connection
- name: Создать группу deploy с GID 1001
group:
name: "{{ deploy_user }}"
@@ -212,6 +259,9 @@
group: "{{ deploy_user }}"
mode: '0600'
- name: Переключиться на пользователя deploy
meta: reset_connection
- name: Клонировать основной репозиторий prod
git:
repo: git@github.com:KerradKerridi/prod.git
@@ -239,6 +289,15 @@
become: yes
become_user: "{{ deploy_user }}"
- name: Установить правильные права на дашборд Node Exporter Full
file:
path: "{{ project_root }}/infra/grafana/provisioning/dashboards/node-exporter-full-dashboard.json"
owner: "{{ deploy_user }}"
group: "{{ deploy_user }}"
mode: '0644'
become: yes
become_user: "{{ deploy_user }}"
- name: Скопировать SSH ключ на старый сервер для копирования файлов
authorized_key:
user: root
@@ -416,22 +475,13 @@
recurse: yes
become: yes
- name: Убедиться, что дашборд Node Exporter Full скопирован
copy:
src: "{{ project_root }}/infra/grafana/provisioning/dashboards/node-exporter-full-dashboard.json"
dest: "{{ project_root }}/infra/grafana/provisioning/dashboards/node-exporter-full-dashboard.json"
owner: "{{ deploy_user }}"
group: "{{ deploy_user }}"
mode: '0644'
become: yes
become_user: "{{ deploy_user }}"
- name: Запустить ботов через make up
command: make up
args:
chdir: "{{ project_root }}"
become: yes
become_user: "{{ deploy_user }}"
tags: ["start_bots"]
# --- НОВОЕ: Проверка портов ---
- name: Пауза на 30 секунд — дать контейнерам запуститься