Ansible is a configuration management tool that makes it easy to automate the deployment and configuration of your infrastructure. You can use it to spin up thousands of servers in record time. This tutorial will demonstrate how to provision multiple Amazon Lightsail Virtual Private Server (VPS) instances and configure them to host a static site with NGINX.
Before we begin
You will need an Amazon AWS account and the latest version of Ansible installed on your machine. The steps in this tutorial are tested on a MacBook Pro with Ansible Version 2.11.4.
Step 1: Create Directory Structure
Create a directory on your machine to store the playbooks and inventory files. Run the following command to create and change into the directory.
mkdir -p ~/ansible/lightsaildemo && cd ~/ansible/lightsaildemo
Step 2: Create Inventory File
Create a file called hosts.yml and add the following YAML config.
There are four tasks in the playbook above. The first task downloads the private key to /tmp and the second makes it so only the current user can read it.
The third task creates the Lightsail VPS and waits for a response containing its public IP address. We are using with_inventory_hostnames so that the task creates an instance for each host in the web section of the inventory file.
The last task stores the hostname and IP address of each VPS instance in your local hosts file.
Step 4: Create NGINX Playbook
Create a file called nginx.yml and add the following YAML config.