G
GuideDevOps
Lesson 13 of 14

Ansible with Cloud & K8s

Part of the Ansible tutorial series.

Overview

Ansible is not just for configuring Linux servers. It has powerful modules for interacting with cloud providers (AWS, Azure, GCP) and Kubernetes clusters.

Managing Cloud Infrastructure

You can use Ansible to provision and manage cloud resources.

Example: Create an AWS EC2 Instance

- name: Create an EC2 instance
  amazon.aws.ec2_instance:
    name: "my-web-server"
    key_name: "my-key"
    instance_type: t2.micro
    image_id: ami-0c55b159cbfafe1f0
    vpc_subnet_id: subnet-12345678

Expected Result:

ok: [localhost] => {
    "changed": true,
    "instance": {
        "instance_id": "i-0abcdef1234567890",
        "state": "running"
    }
}

Managing Kubernetes

With the kubernetes.core collection, you can deploy applications to Kubernetes.

Example: Deploy a Pod

- name: Create a Pod
  kubernetes.core.k8s:
    state: present
    definition:
      apiVersion: v1
      kind: Pod
      metadata:
        name: nginx-pod
      spec:
        containers:
        - name: nginx
          image: nginx:latest

Expected Result:

ok: [localhost] => {
    "changed": true,
    "result": {
        "status": "Success",
        "message": "pod/nginx-pod created"
    }
}