5

---
- hosts: webservers
  become: true
  tasks:
    - name: Установить Nginx
      apt:
        name: nginx
        state: present
      when: ansible_os_family == "Debian"

    - name: Установить Nginx (CentOS)
      yum:
        name: nginx
        state: present
      when: ansible_os_family == "RedHat"

    - name: Запустить Nginx
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Создать директорию для сайта
      file:
        path: /var/www/html/mysite
        state: directory
        mode: 0755

    - name: Скопировать index.html
      copy:
        dest: /var/www/html/mysite/index.html
        content: |
          <html>
          <head>
            <title>My Site</title>
          </head>
          <body>
            <h1>Hello, world!</h1>
          </body>
          </html>

    - name: Заменить конфигурацию Nginx
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/sites-available/mysite
      notify:
        - Перезапустить Nginx

  handlers:
    - name: Перезапустить Nginx
      service:
        name: nginx
        state: restarted