101 lines
3.3 KiB
Terraform
101 lines
3.3 KiB
Terraform
---
|
|
# 1. Валидация и добавление хоста
|
|
- name: Validate input and add host dynamically
|
|
hosts: localhost
|
|
gather_facts: no
|
|
tasks:
|
|
- name: Check if vm_ip is provided
|
|
fail:
|
|
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!"
|
|
when: ansible_user is not defined or ansible_user == ""
|
|
|
|
- name: Display target information
|
|
debug:
|
|
msg: |
|
|
========================================
|
|
Target Configuration:
|
|
- IP Address: {{ vm_ip }}
|
|
- SSH User: {{ ansible_user }}
|
|
========================================
|
|
|
|
- name: Add new VM to inventory with authentication
|
|
add_host:
|
|
name: "{{ vm_ip }}"
|
|
groups: npm_servers
|
|
ansible_user: "{{ ansible_user }}"
|
|
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
|
|
# Добавляем аутентификацию - выберите ОДИН из вариантов ниже:
|
|
|
|
# Вариант 1: Использовать пароль
|
|
ansible_ssh_pass: "{{ ansible_password | default('') }}"
|
|
ansible_become_pass: "{{ ansible_password | default('') }}"
|
|
|
|
# Вариант 2: Использовать SSH ключ (раскомментируйте и закомментируйте вариант 1)
|
|
# ansible_ssh_private_key_file: "{{ ssh_key_path | default('~/.ssh/id_rsa') }}"
|
|
|
|
# 2. Основная настройка NPM
|
|
- name: Setup Minimal Linux with Docker and Nginx Proxy Manager
|
|
hosts: npm_servers
|
|
become: yes
|
|
gather_facts: yes
|
|
tasks:
|
|
- name: Update apt cache and install prerequisites
|
|
apt:
|
|
name: ['apt-transport-https', 'ca-certificates', 'curl', 'gnupg', 'lsb-release']
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Add Docker GPG apt Key
|
|
apt_key:
|
|
url: https://download.docker.com/linux/debian/gpg
|
|
state: present
|
|
|
|
- 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
|
|
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
|
|
systemd:
|
|
name: docker
|
|
state: started
|
|
enabled: yes
|
|
|
|
- name: Create directory for Nginx Proxy Manager
|
|
file:
|
|
path: /opt/npm
|
|
state: directory
|
|
mode: '0755'
|
|
|
|
- name: Create docker-compose.yml for NPM
|
|
copy:
|
|
dest: /opt/npm/docker-compose.yml
|
|
content: |
|
|
version: '3.8'
|
|
services:
|
|
app:
|
|
image: 'jc21/nginx-proxy-manager:latest'
|
|
restart: unless-stopped
|
|
ports:
|
|
- '80:80'
|
|
- '81:81'
|
|
- '443:443'
|
|
volumes:
|
|
- ./data:/data
|
|
- ./letsencrypt:/etc/letsencrypt
|
|
|
|
- name: Run Nginx Proxy Manager via Docker Compose
|
|
community.docker.docker_compose_v2:
|
|
project_src: /opt/npm
|
|
state: present |