Using Ansible with Cloud Providers: AWS, Azure, and GCP

Introduction

As businesses increasingly adopt cloud platforms like AWS, Azure, and Google Cloud (GCP), managing infrastructure efficiently becomes crucial. Ansible, a powerful automation tool, simplifies provisioning, configuration, and management of cloud resources across multiple providers.

In this article, we’ll explore how Ansible integrates with AWS, Azure, and GCP, with examples of automating infrastructure on each platform.

Why Use Ansible for Cloud Automation?

? Agentless: No need to install agents on cloud servers

? Idempotent: Ensures consistent deployments

? Cross-Cloud Support: Works across AWS, Azure, and GCP

? Infrastructure as Code (IaC): Define cloud resources with YAML

Ansible with AWS

Prerequisites

Install AWS CLI:

sudo apt install awscli -y

aws configure

Install Ansible AWS Collection:

ansible-galaxy collection install amazon.aws

IAM Permissions: Ensure the IAM user has EC2FullAccess or relevant policies.

Example: Launch an EC2 Instance

---

- name: Launch AWS EC2 Instance

hosts: localhost

gather_facts: no

tasks:

- name: Create EC2 instance

amazon.aws.ec2_instance:

name: "Ansible-EC2"

key_name: "my-key"

instance_type: "t2.micro"

image_id: "ami-12345678"

region: "us-east-1"

vpc_subnet_id: "subnet-abcdef"

security_group: "default"

state: started

? This playbook launches an EC2 instance in AWS.

Ansible with Azure

Prerequisites

Install Azure CLI:

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

az login

Install Ansible Azure Collection:

ansible-galaxy collection install azure.azcollection

Configure Azure credentials in Ansible:

export AZURE_SUBSCRIPTION_ID="your-subscription-id"

export AZURE_CLIENT_ID="your-client-id"

export AZURE_SECRET="your-secret"

export AZURE_TENANT="your-tenant-id"

Example: Create an Azure Virtual Machine

---

- name: Create Azure VM

hosts: localhost

tasks:

- name: Create Azure Virtual Machine

azure.azcollection.azure_rm_virtualmachine:

resource_group: "AnsibleResourceGroup"

name: "AnsibleVM"

vm_size: "Standard_B1s"

admin_username: "adminuser"

admin_password: "Password123!"

image:

offer: "UbuntuServer"

publisher: "Canonical"

sku: "18.04-LTS"

version: "latest"

location: "East US"

state: present

? This playbook provisions an Azure Virtual Machine.

Ansible with Google Cloud (GCP)

Prerequisites

Install GCP SDK:

sudo apt install google-cloud-sdk -y

gcloud auth application-default login

Install Ansible GCP Collection:

ansible-galaxy collection install google.cloud

Set up Service Account Credentials


  • Create a Service Account with the required permissions.
  • Download the JSON key file.
  • Set the environment variable:


export GOOGLE_CREDENTIALS=~/gcp-key.json

Example: Create a GCP Compute Engine Instance

---

- name: Create GCP VM Instance

hosts: localhost

tasks:

- name: Create GCE instance

google.cloud.gcp_compute_instance:

name: "ansible-instance"

machine_type: "n1-standard-1"

zone: "us-central1-a"

project: "my-gcp-project"

auth_kind: "serviceaccount"

disks:

- auto_delete: true

boot: true

initialize_params:

source_image: "projects/debian-cloud/global/images/family/debian-10"

network_interfaces:

- network: "default"

state: present

? This playbook creates a Google Cloud Compute Engine instance.

Conclusion

Ansible provides a unified approach to automate infrastructure across AWS, Azure, and GCP. With a simple YAML-based configuration, teams can efficiently manage cloud resources, ensuring faster deployments and consistent configurations.

?? Need help automating your cloud infrastructure? Contact HawkStack Technologies for expert DevOps solutions!

For more details click www.hawkstack.com

要查看或添加评论,请登录

Quasher Yasmeen Hussain的更多文章