Automating Server Deployment and Web Applications Using Terraform and Ansible Commands
In this blog, I will explain how I automated the deployment of two servers using Terraform for provisioning and Ansible commands for configuration. One server (server1
) runs an NGINX web server, and the other (server2
) hosts a custom HTML web page. This approach does not use Ansible playbooks, but instead manual Ansible commands to achieve the same result.
Step 1: Provision Servers with Terraform
To begin, we use Terraform to create two servers on AWS EC2:
Terraform Configuration: main.tf
:
provider "aws" { region = "us-east-1" }
resource "aws_instance" "server1" { ami = "ami-0c55b159cbfafe1f0" # Replace with your AMI instance_type = "t2.micro" key_name = "ansible-master"
tags = { Name = "server1" } }
resource "aws_instance" "server2" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" key_name = "ansible-master"
tags = { Name = "server2" } }
output "server1_ip" { value = aws_instance.server1.public_ip }
output "server2_ip" { value = aws_instance.server2.public_ip }
Run Terraform Commands
Execute the following commands:
terraform init
terraform apply
Step 2: SSH Configuration for Ansible
Create Inventory File (hosts
)
Add the servers' IPs to the Ansible inventory:
[servers] server1 ansible_host=<server1_ip> ansible_user=ubuntu ansible_ssh_private_key_file=~/keys/ansible.pem server2 ansible_host=<server2_ip> ansible_user=ubuntu ansible_ssh_private_key_file=~/keys/ansible.pem
Step 3: Configure Servers Using Manual Ansible Commands
Once the inventory is ready, configure the servers using Ansible ad-hoc commands:
1. Install NGINX on server1
Run the following command to install NGINX on server1
:
ansible server1 -i hosts -m apt -a "name=nginx state=present" --become
2. Start and Enable NGINX
Start and enable NGINX on server1
using:
ansible server1 -i hosts -m service -a "name=nginx state=started enabled=true" --become
3. Deploy a Custom Web Page on server2
To deploy a custom HTML file on server2
, use the following command:
ansible server2 -i hosts -m copy -a "dest=/var/www/html/index.html content='My Html code
'" --become
4. Install and Start Apache2 on server2
Install Apache2 on server2
to serve the HTML file:
ansible server2 -i hosts -m apt -a "name=apache2 state=present" --become ansible server2 -i hosts -m service -a "name=apache2 state=started enabled=true" --become
Step 4: Verify the Configuration
Ping Test
To confirm Ansible can connect to the servers, run:
ansible servers -i hosts -m ping:
Test the Web Servers
For
server1
(NGINX Server):
Open a browser and visit:http://<server1_ip
For
server2
(Custom Web Page):
Open a browser and visit:http://<server2_ip>
Step 5: Troubleshoot Common Issues
Issue: Private Key Permissions Too Open
If you encounter:
Permissions 0664 for 'ansible-.pem' are too open
Fix it using:
chmod 400 ~/keys/ansible.pem./
Conclusion
In this blog, I demonstrated how to provision and configure two servers without using Ansible playbooks. By using Terraform to provision infrastructure and Ansible ad-hoc commands to configure servers, we achieved:
NGINX Installation and Configuration on
server1
.Custom Web Page Deployment on
server2
.
This approach is ideal for small-scale automation or when quick configurations are needed.
Try it out, and let me know your thoughts in the comments! ๐