Ansible Automation

Configuration management & automation — Azure · AWS · GCP

Concept

Ansible automates server configuration, application deployment, and cloud provisioning using agentless SSH and YAML playbooks. Cloud modules work across Azure, AWS, and GCP.

Cloud Inventory Plugin
azure.azcollection.azure_rm
amazon.aws.aws_ec2
google.cloud.gcp_compute
Auth Method
Service Principal (AZURE_*)
AWS Profile / IAM Role (AWS_*)
Service Account (JSON key / GCP_AUTH_KIND)
VM Module
azure_rm_virtualmachine
ec2_instance
gcp_compute_instance
Network Module
azure_rm_virtualnetwork
ec2_vpc_net
gcp_compute_network

Ansible Playbook — GCP VM Setup

---
- name: Common VM setup
  hosts: all
  become: yes
  vars:
    app_user: myapp
    app_dir: /opt/myapp
  tasks:
    - name: Install Docker
      apt:
        name: docker.io
        state: present
        update_cache: yes

    - name: Create app user
      user:
        name: "{{ app_user }}"
        state: present
        groups: docker

    - name: Start node_exporter
      systemd:
        name: prometheus-node-exporter
        state: started
        enabled: yes

    - name: Deploy app config
      template:
        src: config.yml.j2
        dest: "{{ app_dir }}/config.yml"

Dynamic Inventory (GCP)

# inventory.gcp.yml
plugin: gcp_compute
projects:
  - my-project
filters:
  - labels.env = dev
hostnames:
  - name
keyed_groups:
  - key: labels.role
auth_kind: serviceaccount
service_account_file: /path/to/sa-key.json

Run Commands

# Dry run (check mode)
ansible-playbook -i inventory.gcp.yml playbook.yml --check

# Apply
ansible-playbook -i inventory.gcp.yml playbook.yml

# Limit to specific group
ansible-playbook -i inventory.gcp.yml playbook.yml --limit "role_webserver"

# Verify idempotency (second run should show changed=0)
ansible-playbook -i inventory.gcp.yml playbook.yml