diff --git a/playbooks/deploy-app1-nginx.yml b/playbooks/deploy-app1-nginx.yml new file mode 100644 index 0000000..caeb5c1 --- /dev/null +++ b/playbooks/deploy-app1-nginx.yml @@ -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 diff --git a/roles/nginx/handlers/main.yml b/roles/nginx/handlers/main.yml new file mode 100644 index 0000000..c7e8dac --- /dev/null +++ b/roles/nginx/handlers/main.yml @@ -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 diff --git a/roles/nginx/tasks/main.yml b/roles/nginx/tasks/main.yml new file mode 100644 index 0000000..a66884c --- /dev/null +++ b/roles/nginx/tasks/main.yml @@ -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 diff --git a/roles/nginx/templates/app1.conf.j2 b/roles/nginx/templates/app1.conf.j2 new file mode 100644 index 0000000..13eab42 --- /dev/null +++ b/roles/nginx/templates/app1.conf.j2 @@ -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; + } +} diff --git a/roles/nginx/templates/index.html.j2 b/roles/nginx/templates/index.html.j2 new file mode 100644 index 0000000..ccc3044 --- /dev/null +++ b/roles/nginx/templates/index.html.j2 @@ -0,0 +1,64 @@ + + +
+Hostname: {{ ansible_hostname }}
+IP Address: {{ ansible_default_ipv4.address }}
+Role: Web Server (Nginx)
+Deployed via: Ansible
+Node Exporter metrics available at: http://{{ ansible_default_ipv4.address }}:9100/metrics
Nginx stub_status at: http://{{ ansible_default_ipv4.address }}/status
Collected by Prometheus: 192.168.0.105:9090