Skip to content

Instantly share code, notes, and snippets.

@suneshgovind
Created December 31, 2019 07:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save suneshgovind/a4ee9700d8b7494993079cb974793270 to your computer and use it in GitHub Desktop.
Save suneshgovind/a4ee9700d8b7494993079cb974793270 to your computer and use it in GitHub Desktop.
Ansible playbook to automate migration of AWS EC2 instances to Instance Metadata Service version 2 (IMDSv2)
---
- name: To enable IMDSv2 in AWS EC2 instances
hosts: localhost
vars:
regions: ["eu-north-1", "ap-south-1", "eu-west-3", "eu-west-2", "eu-west-1", "ap-northeast-2", "ap-northeast-1", "sa-east-1", "ca-central-1", "ap-southeast-1", "ap-southeast-2", "eu-central-1", "us-east-1", "us-east-2", "us-west-1", "us-west-2"]
shell_file: "modify_instance_metadata_options.sh"
tasks:
- name: get instance info
local_action:
module: ec2_instance_info
region: "{{ item }}"
register: instance_info
with_items:
- "{{ regions }}"
- name: Enable IMDSv2 for instances in each region
command: "sh {{ shell_file }} {{ item | join(' ') }}"
with_together :
- "{{ regions }}"
- "{{ instance_info | json_query('results[*].instances[*].instance_id') }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment