Multi-Cloud Environment Deployment with Ansible

Task Overview

This task aims to automate the provisioning of infrastructure and deployment of applications across multiple cloud platforms (AWS, Azure, and GCP) using Ansible. The automation will cover instance provisioning, networking, storage, and application deployment, ensuring a consistent, scalable, and reproducible environment.

Technology Stack

Task Implementation

1. Setting Up the Ansible Control Node

ansible-galaxy collection install amazon.aws google.cloud azure.azcollection

2. Define Multi-Cloud Inventory

Create a dynamic inventory file for AWS, Azure, and GCP.

AWS Inventory Example (aws.yml)


plugin: amazon.aws.aws_ec2
regions:
  - us-east-1
filters:
  instance-state-name: running
keyed_groups:
  - key: tags['Environment']
    prefix: aws
    

Azure Inventory Example (azure.yml)


plugin: azure.azcollection.azure_rm
auth_source: cli
include_vm_resource_groups:
  - myResourceGroup
    

GCP Inventory Example (gcp.yml)


plugin: google.cloud.gcp_compute
auth_kind: serviceaccount
project_id: my-gcp-project
filters:
  - status = RUNNING
    

3. Writing Ansible Playbooks

We create Ansible Playbooks to provision infrastructure and deploy applications.

3.1 Provisioning EC2 Instances on AWS


- name: Provision AWS EC2 Instance
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Launch EC2 Instance
      amazon.aws.ec2_instance:
        name: "MultiCloud-Server"
        region: "us-east-1"
        instance_type: "t2.micro"
        image_id: "ami-0abcdef1234567890"
        key_name: "my-key"
        network:
          assign_public_ip: true
        security_groups: ["multi-cloud-security"]
        tags:
          Environment: "Production"
      register: aws_instance

    - name: Display Instance Public IP
      debug:
        msg: "AWS Instance Public IP: {{ aws_instance.instances[0].public_ip_address }}"
    

3.2 Provisioning Azure Virtual Machines

- name: Provision Azure VM
  hosts: localhost
  tasks:
    - name: Create Azure VM
      azure.azcollection.azure_rm_virtualmachine:
        resource_group: "myResourceGroup"
        name: "MultiCloudAzureVM"
        vm_size: "Standard_B1s"
        admin_username: "azureuser"
        admin_password: "Password123!"
        image:
          offer: "UbuntuServer"
          publisher: "Canonical"
          sku: "18.04-LTS"
          version: "latest"

3.3 Provisioning GCP Compute Instance

- name: Provision GCP Instance
  hosts: localhost
  tasks:
    - name: Create a GCP Instance
      google.cloud.gcp_compute_instance:
        name: "multicloud-gcp-instance"
        zone: "us-central1-a"
        machine_type: "n1-standard-1"
        boot_disk:
          initialize_params:
            image: "projects/debian-cloud/global/images/debian-10"
        network_interfaces:
          - network: "default"
            access_configs:
              - name: "External NAT"
                type: "ONE_TO_ONE_NAT"

4. Deploying Applications

After provisioning, we deploy an Nginx-based web application across all instances.

4.1 Deploy Nginx on AWS, Azure, and GCP


- name: Install and Start Nginx
  hosts: all
  become: yes
  tasks:
    - name: Install Nginx
      package:
        name: nginx
        state: present

    - name: Start Nginx
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Copy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html
    

5. Configuring Security & Access

6. Monitoring Setup

Configure Prometheus & Grafana for monitoring.

CI/CD Pipeline with Jenkins (Optional)

To ensure continuous deployment, we integrate Jenkins with Ansible.

Jenkins Pipeline Example


pipeline {
    agent any
    stages {
        stage('Provision Infrastructure') {
            steps {
                sh 'ansible-playbook provision-infra.yml'
            }
        }
        stage('Deploy Application') {
            steps {
                sh 'ansible-playbook deploy-nginx.yml'
            }
        }
    }
}
    

Testing & Validation

Project Outcomes

Next Steps & Enhancements

This project provides a scalable, automated, and secure way to deploy applications in a multi-cloud environment using Ansible. 🚀