2.1.4 VCN, Terraform and Ansible (Nginx example)
Example for deploying Nginx service on OCI Instance with Terraform and Ansible
This tutorial will show you how to use Terraform and Ansible for deploying an Nginx service on an OCI Instance.
I advise you to read tutorials 2.1.1, 2.1.2 and 2.1.3 before moving on with this one.
For the instance creation with Terraform, take a look at "Terraform - small test"
The purpose of using both Terraform and Ansible
Terraform will implement a VCN and its resources along with a Compute Instance
Ansible will deploy nginx service on the deployed Instance
Prepare environment
For installation of Ansible on Ubuntu, perform following steps:.
1. Check if Python3 is installed and configured:
root@deploymentmachine:/home/terra_and_ansible# python3 --version
Python 3.8.52. Proceed with installation steps (example on Ubuntu):
root@deploymentmachine:/home/terra_and_ansible# apt update
Hit:1 http://repo.mysql.com/apt/ubuntu focal InRelease
Hit:2 http://eu-frankfurt-1-ad-3.clouds.archive.ubuntu.com/ubuntu focal InRelease
[...... snip .....]
root@deploymentmachine:/home/terra_and_ansible#
root@deploymentmachine:/home/terra_and_ansible# apt install software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
[...... snip .....]
root@deploymentmachine:/home/terra_and_ansible# apt-add-repository --yes --update ppa:ansible/ansible
Hit:1 http://repo.mysql.com/apt/ubuntu focal InRelease
Hit:2 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:3 https://packages.grafana.com/oss/deb stable InRelease
[...... snip .....]
root@deploymentmachine:/home/terra_and_ansible# apt install ansible -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
[...... snip .....]3. Check the installed version
Content of my working directory
...and now, let's provide the content of each file (terraform and ansible)
Ansible files, Nginx files and folders for deploying Nginx
The Ansible playbook that will install and configure the Nginx service:
The folder "nginx" contains files "index.html" and "static-site.cfg" that are necessary to configure nginx after its installation:
For ignoring SSH authenticity checking, file ansible.cfg was created
You can also set this as an environment variable:
The Terraform files for provisioning VCN and Instance
1.The content of provider.tf
2. The content of variables.tf (added variables for VCN & Instance creation):
We will create an instance with Ubuntu image. Notice that we allow traffic on ports 22, 8080 and 443. As you probably have noticed earlier, in the static-site.cfg, the default port for nginx has been changed from 80 to 8080.
3. The content of compartment.tf:
This will create a child compartment for the root compartment, named "WildTestCompartment"
4. The content of vcn.tf:
5. Content of subnet.tf:
6. Content of creating an internet gateway,int_gateway.tf:
7. Content of creating security list, security_list.tf:
8. Content of route.tf:
9. Content for creating dhcp options, dhcp_opt.tf:
10. Content for creating the instance, instance.tf
11. The content of data.tf - this file allows us to obtain the primary VNIC ID:
12. The content of remote.tf:
There will be two new provisioners here "local-exec" and "remote-exec".
The "local-exec" will run the ansible playbook to install nginx on the new instance. Make sure you run this as a non-root user (in this case, "ubuntu" user)
Time to deploy
Run the three Terraform commands: "terraform init", "terraform plan" and, if no Terraform syntax errors, "terraform apply"
In the output of "terraform apply" you will see everything related to Ansible.
If no errors, you should see an output of this kind (in order):
>> Output generated by "remote.tf", "remote-exec" provisioner:

>> Output generated by "remote.tf", "local-exec" provisioner:

Perform the checking
Our instance has been successfully deployed, and according to the "terraform apply" output, so has been the nginx service:

If you try to telnet into the public IP of the new instance, and port 8080, you would get the following error:
You can perform a few checks in the OCI UI, just to make sure everything is properly configured:


This is only an issue caused by firewall, that blocks the port 8080.
Login to new instance, and install firewalld:
... and now open port 8080
Let's see if it works now:
... and from a browser of your choice:

Notes
Feel free to automate the installation of firewalld from remote.tf file, at"remote-exec", inline
Destroy VCN resources and instance
Run "terraform destroy" in the working directory:
Last updated