How to Install Nginx and Deploy a Simple Webpage Using Ansible

How to Install Nginx and Deploy a Simple Webpage Using Ansible

  1. Update apt cache The first task updates the apt cache on the server to ensure that the latest version of packages is available for installation.

  2. Install Nginx The second task installs Nginx on the server using the apt module.

  3. Copy sample webpage The third task copies a sample webpage from your local machine to the remote server using the copy module. This is the webpage that will be served by Nginx.

  4. Restart Nginx service The final task restarts the Nginx service to ensure that the changes take effect.

- name: Install Nginx and deploy sample webpage
  hosts: server_3
  become: true

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Copy sample webpage
      copy:
        src: /home/ubuntu/ansible/webpage.html
        dest: /var/www/html/index.html
        mode: "0644"

    - name: Restart Nginx service
      service:
        name: nginx
        state: restarted
ansible-playbook webpage.yml