Merge pull request 'Add nginx role and playbook for App1 deployment' (#4) from feature/nginx-app1-deployment into main

Reviewed-on: #4
This commit is contained in:
2026-02-04 04:41:03 +00:00
5 changed files with 255 additions and 0 deletions

View File

@ -0,0 +1,37 @@
---
- name: Deploy Nginx on App1
hosts: 192.168.0.110
become: yes
gather_facts: yes
roles:
- role: nginx
tasks:
- name: Verify Nginx installation
uri:
url: "http://{{ ansible_default_ipv4.address }}"
status_code: 200
timeout: 10
register: nginx_check
until: nginx_check.status == 200
retries: 5
delay: 5
ignore_errors: yes
- name: Verify Nginx status endpoint
uri:
url: "http://{{ ansible_default_ipv4.address }}/status"
status_code: 200
timeout: 5
register: status_check
ignore_errors: yes
- name: Display deployment result
debug:
msg: |
✅ Nginx successfully deployed on {{ inventory_hostname }}!
🌐 Access at: http://{{ ansible_default_ipv4.address }}
📊 Status page: http://{{ ansible_default_ipv4.address }}/status
📈 Nginx metrics: http://{{ ansible_default_ipv4.address }}/nginx_status
🖥️ Node metrics: http://{{ ansible_default_ipv4.address }}:9100/metrics

View File

@ -0,0 +1,13 @@
---
# Handlers for Nginx role
- name: reload nginx
systemd:
name: nginx
state: reloaded
daemon_reload: yes
- name: restart nginx
systemd:
name: nginx
state: restarted
daemon_reload: yes

113
roles/nginx/tasks/main.yml Normal file
View File

@ -0,0 +1,113 @@
---
# Установка и настройка Nginx - финальная версия
- name: Install prerequisites
apt:
name:
- curl
- wget
- software-properties-common
- ca-certificates
- gnupg2
state: present
update_cache: yes
tags: nginx
- name: Create keyrings directory
file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
tags: nginx
- name: Download and add Nginx GPG key
shell: |
curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor -o /etc/apt/keyrings/nginx.gpg
chmod 644 /etc/apt/keyrings/nginx.gpg
args:
creates: /etc/apt/keyrings/nginx.gpg
tags: nginx
- name: Add Nginx repository
apt_repository:
repo: "deb [signed-by=/etc/apt/keyrings/nginx.gpg] http://nginx.org/packages/ubuntu {{ ansible_distribution_release }} nginx"
state: present
filename: nginx-official
update_cache: yes
tags: nginx
- name: Install Nginx
apt:
name: nginx
state: latest
update_cache: yes
tags: nginx
- name: Create custom web directory
file:
path: /var/www/app1
state: directory
owner: www-data
group: www-data
mode: '0755'
tags: nginx
- name: Deploy test index.html
template:
src: index.html.j2
dest: /var/www/app1/index.html
owner: www-data
group: www-data
mode: '0644'
tags: nginx
- name: Remove default Nginx configurations
file:
path: "{{ item }}"
state: absent
loop:
- /etc/nginx/conf.d/default.conf
- /etc/nginx/conf.d/default.conf.backup
- /etc/nginx/sites-enabled/default
tags: nginx
notify: reload nginx
- name: Deploy Nginx configuration for app1 in conf.d
template:
src: app1.conf.j2
dest: /etc/nginx/conf.d/app1.conf
owner: root
group: root
mode: '0644'
tags: nginx
notify: reload nginx
- name: Remove old sites-available config if exists
file:
path: /etc/nginx/sites-available/app1
state: absent
tags: nginx
- name: Remove old sites-enabled symlink if exists
file:
path: /etc/nginx/sites-enabled/app1
state: absent
tags: nginx
- name: Test Nginx configuration
command: nginx -t
register: nginx_test
changed_when: false
tags: nginx
- name: Display Nginx test result
debug:
msg: "{{ nginx_test.stdout_lines }}"
tags: nginx
- name: Enable and start Nginx service
systemd:
name: nginx
state: started
enabled: yes
daemon_reload: yes
tags: nginx

View File

@ -0,0 +1,28 @@
# App1 Nginx configuration
server {
listen 80;
server_name _;
root /var/www/app1;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.0.0/24;
deny all;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.0.0/24;
deny all;
}
}

View File

@ -0,0 +1,64 @@
<!DOCTYPE html>
<html>
<head>
<title>Test App1 - Nginx</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
background-color: #f4f4f9;
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h1 {
color: #2c3e50;
}
.info {
background-color: #e8f4fc;
padding: 15px;
border-left: 4px solid #3498db;
margin: 20px 0;
}
.metrics {
background-color: #e8f8ef;
padding: 15px;
border-left: 4px solid #2ecc71;
margin: 20px 0;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 Test Application 1 - Nginx Server</h1>
<div class="info">
<h3>📊 Server Information:</h3>
<p><strong>Hostname:</strong> {{ ansible_hostname }}</p>
<p><strong>IP Address:</strong> {{ ansible_default_ipv4.address }}</p>
<p><strong>Role:</strong> Web Server (Nginx)</p>
<p><strong>Deployed via:</strong> Ansible</p>
</div>
<div class="metrics">
<h3>📈 Monitoring:</h3>
<p>Node Exporter metrics available at: <code>http://{{ ansible_default_ipv4.address }}:9100/metrics</code></p>
<p>Nginx stub_status at: <code>http://{{ ansible_default_ipv4.address }}/status</code></p>
<p>Collected by Prometheus: <code>192.168.0.105:9090</code></p>
</div>
<h3>🔗 Infrastructure Links:</h3>
<ul>
<li><a href="http://192.168.0.106:3000">Grafana Dashboard</a></li>
<li><a href="http://192.168.0.105:9090">Prometheus UI</a></li>
<li><a href="http://192.168.0.100:3000">Git/Forgejo</a></li>
</ul>
</div>
</body>
</html>