Prerequisite
- Before you start, you should already know how to write Ansible playbooks well.
- You should have some basic knowledge of Nginx.
- You should be comfortable with AWS EC2.
If you’re new to AWS EC2, check out this beginner-friendly guide. For more information about Ansible, click here.
Create the EC2 Instances
Set up two EC2 instances for this project: one as the control (worker) node, managed by Ansible, and the other as the managed (worker) node. The control node will use playbooks to configure the managed node and automate the entire deployment process.
Let’s write the playbook
What we’re going to do is create a playbook that installs Nginx and starts the service on the worker node. This playbook will be set up and controlled by the control node. Here’s the playbook:
- name: This is a simple HTML project hosts: servers #Group name that will host this playbook become: yes #Giving sudo privileges tasks: - name: nginx-install #This will install the nginx apt: name: nginx state: latest - name: nginx-start service: #This will start and enable the nginx service name: nginx state: started enabled: yes - name: deploy-app copy: src: ../index.html #Give path to the file dest: /var/www/html/ #Nginx will serve our file from this specific location
Our next task is straightforward: run this playbook on the control node using the terminal and wait for Ansible to configure and deploy an end-to-end application with a single click.
If you’d like to access my “index.html” file, just click here.
Execute the playbook
To run the playbook, use the following command:
ansible-playbook <playbook-name>
You’ll see some output similar to this:
Check the project by accessing
Finally, ensure the project is up and running by accessing it on the default Nginx port, which is 80.