Aug 15 2023
Jun 28 2023
Resource
BlogTopic
Edge ComputeDate
May 13, 2020This tutorial is part one of a three-part series that will show you how to use Ansible and Terraform to manage edge compute resources created on StackPath more efficiently, specifically edge VMs.
In this tutorial we will use Ansible to automate the installation of a LEMP stack on an edge VM instance. However, you can use Ansible to automate the installation of other package types as well.
To offer an easy introduction to automating edge VM configurations with Ansible, we will focus on configuring a single instance in this tutorial. In part three of this series, we will use Ansible to configure multiple instances all at once.
Part One: Using Ansible for Edge Compute Resource Automation
Part Two: Using Terraform for Provisioning Edge Compute Resources
Part Three: Using Ansible and Terraform Together
Ansible is a configuration management system used to automate deployments, updates, and systems management of applications in varied environments. The configuration files are easy to understand YAML files written in plain English. The installation of Ansible is simple, and since it communicates with remote systems via SSH there are no agents needed on the remote machines.
For this tutorial you will need a StackPath account. You can register for an account if you don’t already have one.
You can install Ansible via its latest release with your operating system’s package manager (CentOS, Ubuntu, Debian) or with pip
, the Python package manager.
If you use macOS, run the following command in your terminal to install Ansible.
brew install ansible
Execute the following ansible
command to verify Ansible was installed without errors.
$ ansible —version
ansible 2.9.3
config file = None
configured module search path = ['/Users/prhomhyse/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/Cellar/ansible/2.9.3/libexec/lib/python3.8/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.8.1 (default, Dec 27 2019, 18:06:00) [Clang 11.0.0 (clang-1100.0.33.16)]
In the StackPath customer portal, click Create Workload. Add a name for your workload and select VM as Workload Type. We will use Ubuntu 18.04 LTS as the Image.
Click Continue to Settings.
Tick the Add Anycast IP Address option, add relevant ports [22, 80, 443], and add your SSH keys as they will be used to access the server via SSH.
Click Continue to Spec.
For this example, we will leave the Spec as is and skip adding an Additional Volume. What we want to focus on is Deployment Target.
Deployment Target is one of the key features of StackPath’s edge compute platform and what sets edge VMs apart from traditional VMs. It is a group of instances running within up to 20+ points of presence (PoPs) that make up StackPath’s edge infrastructure. Also, the instances in one Development Target can communicate with instances of another Development Target.
For simplicity, we will deploy a single instance in a single PoP (Atlanta). However, you can easily add more instances, PoPs, and Deployment Targets later on.
Click Create Workload to deploy the instance in the specified PoP. This should only take a few seconds.
When the Status column shows a Running state you can proceed to the next step.
The LEMP stack is made of Linux, Nginx, MySQL and PHP. These packages will be installed in the instances.
To install these packages, click Go To Workload and take note of the Public IP Address. This IP Address is what allows you to access the VM from outside of StackPath.
Depending on the Ansible host machine, if the host
file isn’t default in /etc/ansible
you can either use the hosts
file in this GitHub repository or create a custom inventory that looks like the following.
[servers]
jollof-server ansible_host=203.0.113.111
[servers:vars]
ansible_python_interpreter=/usr/bin/python3
To continue, please clone this GitHub repository and cd
into the ansible-playbooks
directory.
cd ansible-playbooks
You can check the Ansible Inventory by running this command that includes a custom host file, adding the -i
flag like this:
ansible-inventory --list -y -i ~/ansible-playbooks/hosts
I added the file’s path since it was a custom one to avoid any errors.
You can also test the SSH connection from your Ansible Host (i.e. your computer) by running the following command.
ansible all -m ping -u ubuntu -i ~/ansible-playbooks/hosts
The default user in Edge VM for Ubuntu is ubuntu
, which is why I used it with the -u
flag instead of root
.
The output from pinging the target is:
jollof-server | SUCCESS => {
"changed": false,
"ping": "pong"
}
This output gives us a green light to run the playbook.
To run the lemp
playbook, change to the lemp
directory.
cd lemp
Use your favorite text editor to edit the content of vars/default.yml
.
mysql_root_password: "your_db_password" #enter your password here
http_host: "example.com" #add your domain here
http_conf: "example.com.conf"
http_port: "80"
Save the file and, in the lemp
directory, run the following command.
ansible-playbook playbook.yml -l jollof-server -u ubuntu -i ~/ansible-playbooks/hosts
You will get an output similar to this:
TASK [Install LEMP Packages] ***************************************************
changed: [jollof-server] => (item=nginx)
changed: [jollof-server] => (item=mysql-server)
changed: [jollof-server] => (item=python3-pymysql)
changed: [jollof-server] => (item=php-fpm)
changed: [jollof-server] => (item=php-mysql)
TASK [Sets Nginx conf file] ****************************************************
changed: [jollof-server]
TASK [Enables new site] ********************************************************
changed: [jollof-server]
TASK [Removes "default" site] **************************************************
changed: [jollof-server]
TASK [Sets the root password] **************************************************
changed: [jollof-server]
TASK [Removes all anonymous user accounts] *************************************
ok: [jollof-server]
TASK [Removes the MySQL test database] *****************************************
ok: [jollof-server]
TASK [UFW - Allow HTTP on port 80] *********************************************
changed: [jollof-server]
TASK [Sets Up PHP Info Page] ***************************************************
changed: [jollof-server]
RUNNING HANDLER [Reload Nginx] *************************************************
changed: [jollof-server]
PLAY RECAP *********************************************************************
jollof-server : ok=12 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
You can make a curl request to the edge VM’s IP to confirm Nginx is installed and running.
curl -I 203.0.113.111
You will get an output similar to this:
HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Wed, 19 Feb 2020 06:52:40 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 19 Feb 2020 06:36:34 GMT
Connection: keep-alive
ETag: "5e4cd772-264"
Accept-Ranges: bytes
In this part of the series, Ansible was used to automate the installation of a LEMP stack on the Workload instance. This saved us from having to manually ssh
into the edge VM and install the packages one after another.
In part two we will use Terraform to provision additional edge VM instances outside of the customer portal.