Aug 15 2023
Jun 28 2023
Resource
BlogTopic
Edge ComputeDate
May 15, 2020This tutorial is part three 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 multiple edge VM instances created with Terraform.
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
To start, clone this GitHub repository and cd
into the ansible-playbooks directory
.
cd ansible-playbooks
If the hosts
file isn’t default in /etc/ansible
, you can either edit the existing hosts
file in this directory or create a new inventory file named hosts
in a different directory that contains the following:
[servers]
jollofx-workload-us-jfk-0 ansible_host=203.0.113.110
jollofx-workload-us-sea-0 ansible_host=203.0.113.112
jollofx-workload-us-jfk-0 ansible_host=203.0.113.113
[servers:vars]
ansible_ssh_private_key_file=/home/user/.ssh/id_rsa
ansible_python_interpreter=/usr/bin/python3
When managing multiple instances versus a single instance with Ansible, you have to add the path to your private key to the ansible_ssh_private_key_file
variable.
To reduce ssh-related errors, it is better to use a passphrase-less key as you may be required to enter your passphrase multiple times according to the number of servers.
You can also manually add the instances’ public IP addresses to your host machine’s known_host
file so Ansible can run without attempting to add the IP addresses automatically.
Now check the Ansible Inventory by running this:
ansible-inventory --list -y -i ~/ansible-playbooks/hosts
Your output will look like this:
all:
children:
servers:
hosts:
jollofx-workload-us-jfk-0:
ansible_host: 203.0.113.110
ansible_python_interpreter: /usr/bin/python3
ansible_ssh_private_key_file: /home/user/.ssh/id_rsa
jollofx-workload-us-sea-0:
ansible_host: 203.0.113.112
ansible_python_interpreter: /usr/bin/python3
ansible_ssh_private_key_file: /home/user/.ssh/id_rsa
jollofx-workload-us-jfk-0:
ansible_host: 203.0.113.113
ansible_python_interpreter: /usr/bin/python3
ansible_ssh_private_key_file: /home/user/.ssh/id_rsa
ungrouped: {}
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:
ansible-playbook playbook.yml -u ubuntu -i ~/ansible-playbooks/hosts
The output is quite lengthy because this time multiple instances are being automated.
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [jollofx-workload-us-jfk-0]
ok: [jollofx-workload-us-sea-0]
ok: [jollofx-workload-us-jfk-0]
TASK [Install Prerequisites] ***************************************************
changed: [jollofx-workload-us-jfk-0] => (item=aptitude)
changed: [jollofx-workload-us-sea-0] => (item=aptitude)
changed: [jollofx-workload-us-jfk-0] => (item=aptitude)
TASK [Install LEMP Packages] ***************************************************
Ansible performs the tasks specified in the playbook and ends with the following:
PLAY RECAP *********************************************************************
jollofx-workload-us-jfk-0 : ok=12 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
jollofx-workload-us-sea-0 : ok=12 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
jollofx-workload-us-jfk-0 : 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.112
Your output will look like this:
HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Tue, 31 Mar 2020 21:34:50 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 31 Mar 2020 21:01:48 GMT
Connection: keep-alive
ETag: "5e83afbc-264"
Accept-Ranges: bytes
In this final tutorial of the series, we explored the use of Ansible to automate the installation of a LEMP stack on all three instances created using Terraform.