--- # Основные задачи для роли nginx # Автор: Сергей Антропов # Сайт: https://devops.org.ru - name: Установка nginx на Ubuntu/Debian apt: name: "{{ nginx_ubuntu_packages }}" state: present update_cache: true when: ansible_os_family == "Debian" tags: - nginx - install - debian - name: Установка nginx на RHEL/CentOS yum: name: "{{ nginx_rhel_packages }}" state: present when: ansible_os_family == "RedHat" tags: - nginx - install - rhel - name: Включение и запуск nginx на Ubuntu/Debian systemd: name: nginx enabled: true state: started when: ansible_os_family == "Debian" tags: - nginx - service - debian - name: Включение и запуск nginx на RHEL/CentOS systemd: name: nginx enabled: true state: started when: ansible_os_family == "RedHat" tags: - nginx - service - rhel - name: Создание директории для веб-контента file: path: "{{ nginx_root_dir }}" state: directory owner: "{{ nginx_user }}" group: "{{ nginx_user }}" mode: '0755' tags: - nginx - config - directories - name: Создание тестовой страницы copy: content: | Nginx Test Page

Nginx работает!

Сервер: {{ ansible_hostname }}

ОС: {{ ansible_distribution }} \ {{ ansible_distribution_version }}

Время: {{ ansible_date_time.iso8601 }}

Роль: nginx (Сергей Антропов)

dest: "{{ nginx_root_dir }}/{{ nginx_index_file }}" owner: "{{ nginx_user }}" group: "{{ nginx_user }}" mode: '0644' notify: restart nginx tags: - nginx - config - content - name: Создание резервной копии конфигурации nginx copy: src: "{{ item }}" dest: "{{ item }}.backup" remote_src: true mode: '0644' owner: root group: root loop: - /etc/nginx/nginx.conf - /etc/nginx/sites-available/default ignore_errors: true when: ansible_os_family == "Debian" tags: - nginx - config - backup - name: Создание резервной копии конфигурации nginx (RHEL) copy: src: "{{ item }}" dest: "{{ item }}.backup" remote_src: true mode: '0644' owner: root group: root loop: - /etc/nginx/nginx.conf - /etc/nginx/conf.d/default.conf ignore_errors: true when: ansible_os_family == "RedHat" tags: - nginx - config - backup - name: Настройка основной конфигурации nginx template: src: nginx.conf.j2 dest: /etc/nginx/nginx.conf owner: root group: root mode: '0644' backup: true notify: restart nginx tags: - nginx - config - main - name: Настройка виртуального хоста (Ubuntu/Debian) template: src: default.conf.j2 dest: /etc/nginx/sites-available/default owner: root group: root mode: '0644' backup: true when: ansible_os_family == "Debian" notify: restart nginx tags: - nginx - config - vhost - debian - name: Настройка виртуального хоста (RHEL/CentOS) template: src: default.conf.j2 dest: /etc/nginx/conf.d/default.conf owner: root group: root mode: '0644' backup: true when: ansible_os_family == "RedHat" notify: restart nginx tags: - nginx - config - vhost - rhel - name: Проверка конфигурации nginx command: nginx -t register: nginx_config_test changed_when: false tags: - nginx - config - test - name: Показать результат проверки конфигурации debug: msg: "{{ nginx_config_test.stdout_lines }}" when: nginx_config_test.stdout_lines is defined tags: - nginx - config - test