--- # Установка и настройка 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