diff --git a/ansible/npm-setup.yml b/ansible/npm-setup.yml index be145e6..e1673db 100644 --- a/ansible/npm-setup.yml +++ b/ansible/npm-setup.yml @@ -1,27 +1,31 @@ --- -# 1. Проверка наличия переменных и динамическое добавление хоста - name: Validate input and add host dynamically hosts: localhost gather_facts: no + vars: + # Пытаемся определить метод аутентификации + auth_method: "{{ 'key' if ssh_key_path is defined else 'password' }}" + tasks: - name: Check if vm_ip is provided fail: - msg: | - ======================================== - ERROR: VM IP address is required! - - Please provide vm_ip variable when running this playbook. - - Example in Semaphore: - - Add vm_ip in Extra Variables - ======================================== + msg: "ERROR: VM IP address is required!" when: vm_ip is not defined or vm_ip == "" - name: Check if ansible_user is provided fail: - msg: "ERROR: ansible_user is required! Please provide SSH username." + msg: "ERROR: ansible_user is required!" when: ansible_user is not defined or ansible_user == "" + - name: Check authentication method + fail: + msg: | + ERROR: Neither password nor SSH key provided! + Please provide either: + - ansible_password variable for password auth + - ssh_key_path variable for key auth + when: ansible_password is not defined and ssh_key_path is not defined + - name: Display target information debug: msg: | @@ -29,72 +33,75 @@ Target Configuration: - IP Address: {{ vm_ip }} - SSH User: {{ ansible_user }} + - Auth Method: {{ 'SSH Key' if ssh_key_path is defined else 'Password' }} ======================================== - - name: Add new VM to inventory + - name: Add host with password authentication add_host: name: "{{ vm_ip }}" groups: npm_servers ansible_user: "{{ ansible_user }}" + ansible_ssh_pass: "{{ ansible_password }}" + ansible_become_pass: "{{ ansible_password }}" ansible_ssh_common_args: '-o StrictHostKeyChecking=no' - ansible_become: yes + when: ansible_password is defined + + - name: Add host with key authentication + add_host: + name: "{{ vm_ip }}" + groups: npm_servers + ansible_user: "{{ ansible_user }}" + ansible_ssh_private_key_file: "{{ ssh_key_path }}" + ansible_ssh_common_args: '-o StrictHostKeyChecking=no' + when: ssh_key_path is defined -# 2. Основная настройка NPM - name: Setup Minimal Linux with Docker and Nginx Proxy Manager hosts: npm_servers become: yes gather_facts: yes tasks: - - name: Display facts about target - debug: - msg: "Setting up Docker and NPM on {{ ansible_hostname }} ({{ ansible_default_ipv4.address }})" - - - name: Update apt cache and install prerequisites + # ... остальные задачи как выше ... + - name: Test connection + ping: + + - name: Update apt cache + apt: + update_cache: yes + + - name: Install Docker prerequisites apt: name: ['apt-transport-https', 'ca-certificates', 'curl', 'gnupg', 'lsb-release'] state: present - update_cache: yes - - name: Check system architecture - debug: - msg: "System architecture: {{ ansible_architecture }}" - - - name: Add Docker GPG apt Key + - name: Add Docker GPG key apt_key: url: https://download.docker.com/linux/debian/gpg state: present - - name: Add Docker Repository + - name: Add Docker repository apt_repository: repo: "deb [arch=amd64] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable" state: present - - name: Install Docker and Docker Compose Plugin + - name: Install Docker apt: name: ['docker-ce', 'docker-ce-cli', 'containerd.io', 'docker-compose-plugin'] state: present update_cache: yes - - name: Ensure Docker service is running and enabled + - name: Start Docker systemd: name: docker state: started enabled: yes - - name: Add user to docker group - user: - name: "{{ ansible_user }}" - groups: docker - append: yes - when: ansible_user != 'root' - - - name: Create directory for Nginx Proxy Manager + - name: Setup Nginx Proxy Manager file: path: /opt/npm state: directory mode: '0755' - - name: Create docker-compose.yml for NPM + - name: Deploy NPM compose file copy: dest: /opt/npm/docker-compose.yml content: | @@ -111,23 +118,17 @@ - ./data:/data - ./letsencrypt:/etc/letsencrypt - - name: Run Nginx Proxy Manager via Docker Compose + - name: Start NPM community.docker.docker_compose_v2: project_src: /opt/npm state: present - - name: Display access information + - name: Success message debug: msg: | ======================================== - ✅ Nginx Proxy Manager is ready! + ✅ Docker and Nginx Proxy Manager installed! - Access URLs: - - Admin Panel: http://{{ vm_ip }}:81 - - HTTP Proxy: http://{{ vm_ip }} - - HTTPS Proxy: https://{{ vm_ip }} - - Default Admin Credentials (change on first login): - - Email: admin@example.com - - Password: changeme + Access NPM Admin: http://{{ vm_ip }}:81 + Default login: admin@example.com / changeme ======================================== \ No newline at end of file