DEV Community

Kishore Suzil
Kishore Suzil

Posted on

πŸš€ Ansible Real-Time Project:

Automating AWS EC2 Management

Overview

This project demonstrates how to use Ansible to automate the provisioning and management of AWS EC2 instances. The tasks include:
βœ… Creating multiple EC2 instances using loops

βœ… Setting up passwordless authentication

βœ… Automating the shutdown of Ubuntu instances using conditionals


πŸ› οΈ Prerequisites

Before running the playbooks, ensure you have:

  1. Ansible Installed on the control node.
  2. AWS CLI Configured with an IAM user having EC2 permissions.
  3. Boto3 and botocore installed using:
   pip install boto3 botocore
Enter fullscreen mode Exit fullscreen mode
  1. SSH Key Pair for authentication.

πŸ“Œ Task 1: Create EC2 Instances using Ansible Loops

We will create:

  • 2 Ubuntu instances
  • 1 CentOS instance

Playbook: create_instances.yml

- hosts: localhost
  connection: local

  tasks:
  - name: Create EC2 instances
    amazon.aws.ec2_instance:
      name: "{{ item.name }}"
      key_name: "key.pem"
      instance_type: t2.micro
      security_group: default
      region: ap-south-1
      aws_access_key: "{{ec2_access_key}}"  # From vault as defined
      aws_secret_key: "{{ec2_secret_key}}"  # From vault as defined      
      network:
        assign_public_ip: true
      image_id: "{{ item.image }}"
      tags:
        environment: "{{ item.name }}"
    loop:
      - { image: "ami-0e1d06225679bc1c5", name: "manage-node-1" } # Update AMI ID according 
      - { image: "ami-0f58b397bc5c1f2e8", name: "manage-node-2" } # to your account
      - { image: "ami-0f58b397bc5c1f2e8", name: "manage-node-3" }
Enter fullscreen mode Exit fullscreen mode

Run the playbook:

ansible-playbook create_instances.yml
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Task 2: Set Up Passwordless Authentication

After launching the instances, we will fetch their public IPs and configure SSH key-based authentication.

Playbook: setup_passwordless_auth.yml

- name: Configure passwordless authentication
  hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - name: Fetch Instance Public IPs
      amazon.aws.ec2_instance_info:
        region: "us-east-1"
      register: ec2_info

    - name: Extract Public IPs
      set_fact:
        ubuntu_ips: "{{ ec2_info.instances | selectattr('image_id', 'equalto', 'ami-0c55b159cbfafe1f0') | map(attribute='public_ip_address') | list }}"
        centos_ip: "{{ ec2_info.instances | selectattr('image_id', 'equalto', 'ami-0c322300a1dd5dc79') | map(attribute='public_ip_address') | first }}"

    - name: Copy SSH Key to Instances
      ansible.builtin.shell: ssh-copy-id -f -o IdentityFile=<Add the path of the key> ubuntu@{{ item }}
      loop: "{{ ubuntu_ips }}"
      ignore_errors: yes

    - name: Copy SSH Key to CentOS Instance
      ansible.builtin.shell: ssh-copy-id -f -o IdentityFile=<Add the path of the key> centos@{{ centos_ip }}
      ignore_errors: yes
Enter fullscreen mode Exit fullscreen mode

Run the playbook:

ansible-playbook setup_passwordless_auth.yml
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Task 3: Automate Shutdown of Ubuntu Instances Using Conditionals

We will use Ansible’s when condition to target only Ubuntu instances for shutdown.

Playbook: shutdown_ubuntu.yml

- name: Shutdown Ubuntu Instances
  hosts: all
  gather_facts: yes
  tasks:
    - name: Shutdown Ubuntu servers
      ansible.builtin.command: shutdown -h now
      when: ansible_facts['distribution'] == "Ubuntu"
Enter fullscreen mode Exit fullscreen mode

Run the playbook:

ansible-playbook -i inventory shutdown_ubuntu.yml
Enter fullscreen mode Exit fullscreen mode

🎯 Summary

βœ… Created 3 EC2 instances using Ansible loops

βœ… Set up passwordless authentication

βœ… Automated shutdown of Ubuntu instances using conditionals

Top comments (0)