--- - name: Подготовить Python для Ansible ansible.builtin.raw: DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y python3 python3-apt changed_when: false - name: Собрать факты ansible.builtin.setup: - name: Установить hostname хоста ansible.builtin.hostname: name: "{{ inventory_hostname }}" failed_when: false - name: Обеспечить запись inventory_hostname в /etc/hosts ansible.builtin.lineinfile: path: /etc/hosts line: "127.0.1.1 {{ inventory_hostname }}" regexp: '^127\.0\.1\.1\s+' state: present failed_when: false - name: Создать каталог .ssh ansible.builtin.file: path: /root/.ssh state: directory owner: root group: root mode: "0700" - name: Добавить авторизованные SSH-ключи ansible.posix.authorized_key: user: root key: "{{ item }}" state: present loop: "{{ host_authorized_keys }}" - name: Удалить cloud-init настройки пароля SSH ansible.builtin.file: path: /etc/ssh/sshd_config.d/50-cloud-init.conf state: absent notify: Перезапустить SSH - name: Отключить вход по паролю в drop-in sshd_config.d ansible.builtin.copy: dest: /etc/ssh/sshd_config.d/00-disable-passwords.conf content: | PasswordAuthentication no KbdInteractiveAuthentication no owner: root group: root mode: "0644" notify: Перезапустить SSH - name: Удалить устаревший drop-in sshd_config.d ansible.builtin.file: path: /etc/ssh/sshd_config.d/99-disable-passwords.conf state: absent notify: Перезапустить SSH - name: Запретить PasswordAuthentication в основном sshd_config ansible.builtin.lineinfile: path: /etc/ssh/sshd_config regexp: '^(#\s*)?PasswordAuthentication\s+.*' line: 'PasswordAuthentication no' notify: Перезапустить SSH - name: Применить настройки SSH ansible.builtin.meta: flush_handlers - name: Поставить системные пакеты и Docker ansible.builtin.apt: name: - docker.io - docker-compose - ufw - ca-certificates - curl - prometheus-node-exporter state: present update_cache: true - name: Включить Docker ansible.builtin.systemd: name: docker enabled: true state: started - name: Остановить устаревший контейнер node-exporter (если есть) ansible.builtin.command: docker rm -f monitoring-node-exporter register: rm_legacy_exporter failed_when: false changed_when: rm_legacy_exporter.rc == 0 - name: Слушать node-exporter только локально ansible.builtin.copy: dest: /etc/default/prometheus-node-exporter content: | ARGS="--web.listen-address={{ monitoring_node_exporter_listen }}" owner: root group: root mode: "0644" register: node_exporter_args when: not (monitoring_node_exporter_public | bool) - name: Включить prometheus-node-exporter ansible.builtin.systemd: name: prometheus-node-exporter enabled: true state: "{{ 'restarted' if (node_exporter_args is changed) else 'started' }}" - name: Разрешить SSH ansible.builtin.command: ufw allow OpenSSH register: ufw_ssh changed_when: "'added' in (ufw_ssh.stdout | lower)" when: ansible_virtualization_type | default('') != 'lxc' - name: Разрешить порт метрик node-exporter в firewall ansible.builtin.command: ufw allow 9100/tcp register: ufw_metrics changed_when: "'added' in (ufw_metrics.stdout | lower)" when: - ansible_virtualization_type | default('') != 'lxc' - monitoring_node_exporter_public | bool - name: Закрыть порт метрик node-exporter снаружи ansible.builtin.command: ufw --force delete allow 9100/tcp register: ufw_metrics_del changed_when: "'deleted' in (stdout | default('') | lower)" failed_when: false loop: [1, 2] when: - ansible_virtualization_type | default('') != 'lxc' - not (monitoring_node_exporter_public | bool) - name: Проверить состояние firewall ansible.builtin.command: ufw status register: ufw_status changed_when: false when: ansible_virtualization_type | default('') != 'lxc' - name: Включить firewall ansible.builtin.command: ufw --force enable when: - ansible_virtualization_type | default('') != 'lxc' - "'Status: active' not in ufw_status.stdout" changed_when: true