134 lines
4.0 KiB
YAML
134 lines
4.0 KiB
YAML
---
|
|
- 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!"
|
|
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: 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: |
|
|
========================================
|
|
Target Configuration:
|
|
- IP Address: {{ vm_ip }}
|
|
- SSH User: {{ ansible_user }}
|
|
- Auth Method: {{ 'SSH Key' if ssh_key_path is defined else 'Password' }}
|
|
========================================
|
|
|
|
- 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'
|
|
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
|
|
|
|
- name: Setup Minimal Linux with Docker and Nginx Proxy Manager
|
|
hosts: npm_servers
|
|
become: yes
|
|
gather_facts: yes
|
|
tasks:
|
|
# ... остальные задачи как выше ...
|
|
- 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
|
|
|
|
- name: Add Docker GPG 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
|
|
apt:
|
|
name: ['docker-ce', 'docker-ce-cli', 'containerd.io', 'docker-compose-plugin']
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Start Docker
|
|
systemd:
|
|
name: docker
|
|
state: started
|
|
enabled: yes
|
|
|
|
- name: Setup Nginx Proxy Manager
|
|
file:
|
|
path: /opt/npm
|
|
state: directory
|
|
mode: '0755'
|
|
|
|
- name: Deploy NPM compose file
|
|
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: Start NPM
|
|
community.docker.docker_compose_v2:
|
|
project_src: /opt/npm
|
|
state: present
|
|
|
|
- name: Success message
|
|
debug:
|
|
msg: |
|
|
========================================
|
|
✅ Docker and Nginx Proxy Manager installed!
|
|
|
|
Access NPM Admin: http://{{ vm_ip }}:81
|
|
Default login: admin@example.com / changeme
|
|
======================================== |