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:
- Ansible Installed on the control node.
- AWS CLI Configured with an IAM user having EC2 permissions.
- Boto3 and botocore installed using:
pip install boto3 botocore
- 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" }
Run the playbook:
ansible-playbook create_instances.yml
π 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
Run the playbook:
ansible-playbook setup_passwordless_auth.yml
π 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"
Run the playbook:
ansible-playbook -i inventory shutdown_ubuntu.yml
π― Summary
β
Created 3 EC2 instances using Ansible loops
β
Set up passwordless authentication
β
Automated shutdown of Ubuntu instances using conditionals
Top comments (0)