Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ksatirli/bcef5f7b01ae937969571541578d5e1e to your computer and use it in GitHub Desktop.
Save ksatirli/bcef5f7b01ae937969571541578d5e1e to your computer and use it in GitHub Desktop.
Toggle EC2 Instance Termination Protection
---
- name: Toggle EC2 Instance Termination Protection
hosts: all
connection: local
gather_facts: false
pre_tasks:
- name:
fail:
msg: Missing required variable `aws_access_key`
when: aws_access_key is undefined
- name:
fail:
msg: Missing required variable `aws_secret_key`
when: aws_secret_key is undefined
- name:
fail:
msg: Missing required variable `instance_ids`
when: instance_ids is undefined
- name:
fail:
msg: Missing required variable `region`
when: region is undefined
tasks:
- name: setting variable to `true` for EC2 API Termination Protection toggle
set_fact:
protection_string: "disable-api-termination"
when: termination_protection == "true"
- name: setting variable to `false` for EC2 API Termination Protection toggle
set_fact:
protection_string: "no-disable-api-termination"
when: termination_protection == "false"
- name: toggling EC2 API Termination Protection
shell: |
aws \
ec2 \
modify-instance-attribute \
--{{ protection_string | quote }} \
--instance-id="{{ item }}"
environment:
AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
AWS_REGION: "{{ region }}"
with_items: "{{ instance_ids }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment