Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Ansible: How to terminate all AWS EC2 instances using Ansible script?

$
0
0

Terminate AWS EC2 instances using Ansible

As by now you probably know, I’ve been working on Ansible a lot lately. In this tutorial we will go over how to terminate all AWS EC2 instances programmatically using Ansible script.

Before you go over this tutorial, make sure you have setup Ansible correctly on your laptop/desktop.

On Crunchify, we have more than 30 Ansible tutorials and those are getting so much love from readers/users.

Let’s get started:

Step-1:

Make sure you have setup ansible correctly on your site.

Step-2

Spawn few EC2 instances.

How to Create, Start and Configure Amazon EC2 instance using simple Ansible Script? (spawn VM remotely)

Step-3

Terminate newly spawned EC2 instances.

Create file crunchify_ec2_terminate.yml.

---
- hosts: localhost
  connection: local
  tasks:
    - name: Terminate Instances
      ec2_instance_facts:
        region: us-east-2
      register: ec2_info

    - name: displaying output
      debug: msg="{{item.instance_id}}"
      with_items: "{{ec2_info.instances}}"

    - name: terminate
      ec2:
        instance_ids: "{{item.instance_id}}"
        region: us-east-2
        state: absent
        wait: yes
      with_items: "{{ec2_info.instances}}"

  • ec2_instance_facts module gathers facts about ec2 instances in AWS.
  • ec2 module is use to create, terminate, start or stop an instance in ec2.

State:

  • absent
  • restarted
  • running
  • stopped

Ansible EC2 module state

Execute command:

command: ansible-playbook -vvv crunchify_ec2_terminate.yml

Console Output:

bash-3.2$ ansible-playbook -vvv crunchify_ec2_terminate.yml 

ansible-playbook 2.9.9
  config file = /Users/crunchify/Documents/ansible.cfg
  configured module search path = [u'/Users/crunchify/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Library/Python/2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 2.7.16 (default, Apr 17 2020, 18:29:03) [GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc-
Using /Users/crunchify/Documents/ansible.cfg as config file
host_list declined parsing /Users/crunchify/Documents/hosts as it did not pass its verify_file() method
script declined parsing /Users/crunchify/Documents/hosts as it did not pass its verify_file() method
auto declined parsing /Users/crunchify/Documents/hosts as it did not pass its verify_file() method
Parsed /Users/crunchify/Documents/hosts inventory source with ini plugin
[DEPRECATION WARNING]: ec2_instance_facts is kept for backwards compatibility but usage is discouraged. The module documentation details page may explain more 
about this rationale.. This feature will be removed in a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in 
ansible.cfg.

PLAYBOOK: crunchify_ec2_terminate.yml **************************************************************************************************************************
1 plays in crunchify_ec2_terminate.yml

PLAY [localhost] ***********************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************
task path: /Users/crunchify/Documents/crunchify_ec2_terminate.yml:2
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: crunchify
<localhost> EXEC /bin/sh -c 'echo ~crunchify && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp `"&& mkdir /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.11-41204-111388218507638 && echo ansible-tmp-1594496997.11-41204-111388218507638="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.11-41204-111388218507638 `" ) && sleep 0'
Using module file /Library/Python/2.7/site-packages/ansible/modules/system/setup.py
<localhost> PUT /Users/crunchify/.ansible/tmp/ansible-local-41195wpzOAR/tmpE3kiFK TO /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.11-41204-111388218507638/AnsiballZ_setup.py
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.11-41204-111388218507638/ /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.11-41204-111388218507638/AnsiballZ_setup.py && sleep 0'
<localhost> EXEC /bin/sh -c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.11-41204-111388218507638/AnsiballZ_setup.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.11-41204-111388218507638/ > /dev/null 2>&1 && sleep 0'
ok: [localhost]
META: ran handlers

TASK [Terminate Instances] *************************************************************************************************************************************
task path: /Users/crunchify/Documents/crunchify_ec2_terminate.yml:5
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: crunchify
<localhost> EXEC /bin/sh -c 'echo ~crunchify && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp `"&& mkdir /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.87-41237-206622508678909 && echo ansible-tmp-1594496997.87-41237-206622508678909="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.87-41237-206622508678909 `" ) && sleep 0'
Using module file /Library/Python/2.7/site-packages/ansible/modules/cloud/amazon/_ec2_instance_facts.py
<localhost> PUT /Users/crunchify/.ansible/tmp/ansible-local-41195wpzOAR/tmpVucbR7 TO /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.87-41237-206622508678909/AnsiballZ__ec2_instance_facts.py
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.87-41237-206622508678909/ /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.87-41237-206622508678909/AnsiballZ__ec2_instance_facts.py && sleep 0'
<localhost> EXEC /bin/sh -c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.87-41237-206622508678909/AnsiballZ__ec2_instance_facts.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1594496997.87-41237-206622508678909/ > /dev/null 2>&1 && sleep 0'
[DEPRECATION WARNING]: The 'ec2_instance_facts' module has been renamed to 'ec2_instance_info'. This feature will be removed in version 2.13. Deprecation 
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [localhost] => {
    "changed": false, 
    "instances": [
        {
            "ami_launch_index": 1, 
            "architecture": "x86_64", 
            "block_device_mappings": [], 
            "capacity_reservation_specification": {
                "capacity_reservation_preference": "open"
            }, 
            "client_token": "", 
            "cpu_options": {
                "core_count": 2, 
                "threads_per_core": 2
            }, 
            "ebs_optimized": false, 
            "ena_support": true, 
            "hibernation_options": {
                "configured": false
            }, 
            "hypervisor": "xen", 
            "image_id": "ami-92swq93023e0b9cb5", 
            "instance_id": "i-04d7c05d0d8af035e", 
            "instance_type": "m5ad.xlarge", 
            "key_name": "crunchifypem", 
            "launch_time": "2020-07-11T19:09:41+00:00", 
            "metadata_options": {
                "http_endpoint": "enabled", 
                "http_put_response_hop_limit": 1, 
                "http_tokens": "optional", 
                "state": "pending"
            }, 
            "monitoring": {
                "state": "disabled"
            }, 
            "network_interfaces": [], 
            "placement": {
                "availability_zone": "us-east-2a", 
                "group_name": "", 
                "tenancy": "default"
            }, 
            "private_dns_name": "", 
            "product_codes": [], 
            "public_dns_name": "", 
            "root_device_name": "/dev/sda1", 
            "root_device_type": "ebs", 
            "security_groups": [], 
            "state": {
                "code": 48, 
                "name": "terminated"
            }, 
            "state_reason": {
                "code": "Client.UserInitiatedShutdown", 
                "message": "Client.UserInitiatedShutdown: User initiated shutdown"
            }, 
            "state_transition_reason": "User initiated (2020-07-11 19:13:48 GMT)", 
            "tags": {
                "Name": "crunchify_vms"
            }, 
            "virtualization_type": "hvm"
        }, 
        {
            "ami_launch_index": 0, 
            "architecture": "x86_64", 
            "block_device_mappings": [], 
            "capacity_reservation_specification": {
                "capacity_reservation_preference": "open"
            }, 
            "client_token": "", 
            "cpu_options": {
                "core_count": 2, 
                "threads_per_core": 2
            }, 
            "ebs_optimized": false, 
            "ena_support": true, 
            "hibernation_options": {
                "configured": false
            }, 
            "hypervisor": "xen", 
            "image_id": "ami-92swq93023e0b9cb5", 
            "instance_id": "i-0543671e6acaf2326", 
            "instance_type": "m5ad.xlarge", 
            "key_name": "crunchifypem", 
            "launch_time": "2020-07-11T19:09:41+00:00", 
            "metadata_options": {
                "http_endpoint": "enabled", 
                "http_put_response_hop_limit": 1, 
                "http_tokens": "optional", 
                "state": "pending"
            }, 
            "monitoring": {
                "state": "disabled"
            }, 
            "network_interfaces": [], 
            "placement": {
                "availability_zone": "us-east-2a", 
                "group_name": "", 
                "tenancy": "default"
            }, 
            "private_dns_name": "", 
            "product_codes": [], 
            "public_dns_name": "", 
            "root_device_name": "/dev/sda1", 
            "root_device_type": "ebs", 
            "security_groups": [], 
            "state": {
                "code": 48, 
                "name": "terminated"
            }, 
            "state_reason": {
                "code": "Client.UserInitiatedShutdown", 
                "message": "Client.UserInitiatedShutdown: User initiated shutdown"
            }, 
            "state_transition_reason": "User initiated (2020-07-11 19:14:20 GMT)", 
            "tags": {
                "Name": "crunchify_vms"
            }, 
            "virtualization_type": "hvm"
        }
    ], 
    "invocation": {
        "module_args": {
            "aws_access_key": null, 
            "aws_secret_key": null, 
            "debug_botocore_endpoint_logs": false, 
            "ec2_url": null, 
            "filters": {}, 
            "instance_ids": [], 
            "profile": null, 
            "region": "us-east-2", 
            "security_token": null, 
            "validate_certs": true
        }
    }
}

TASK [displaying output] ****************************************************************************************************************************************
task path: /Users/crunchify/Documents/crunchify_ec2_terminate.yml:10
ok: [localhost] => (item={u'network_interfaces': [], u'root_device_type': u'ebs', u'private_dns_name': u'', u'tags': {u'Name': u'crunchify_vms'}, u'key_name': u'crunchifypem', u'image_id': u'ami-92swq93023e0b9cb5', u'cpu_options': {u'threads_per_core': 2, u'core_count': 2}, u'state_transition_reason': u'User initiated (2020-07-11 19:13:48 GMT)', u'hibernation_options': {u'configured': False}, u'capacity_reservation_specification': {u'capacity_reservation_preference': u'open'}, u'public_dns_name': u'', u'placement': {u'availability_zone': u'us-east-2a', u'tenancy': u'default', u'group_name': u''}, u'security_groups': [], u'block_device_mappings': [], u'metadata_options': {u'http_endpoint': u'enabled', u'state': u'pending', u'http_tokens': u'optional', u'http_put_response_hop_limit': 1}, u'state_reason': {u'message': u'Client.UserInitiatedShutdown: User initiated shutdown', u'code': u'Client.UserInitiatedShutdown'}, u'product_codes': [], u'monitoring': {u'state': u'disabled'}, u'ami_launch_index': 1, u'ena_support': True, u'ebs_optimized': False, u'launch_time': u'2020-07-11T19:09:41+00:00', u'instance_id': u'i-04d7c05d0d8af035e', u'instance_type': u'm5ad.xlarge', u'state': {u'code': 48, u'name': u'terminated'}, u'root_device_name': u'/dev/sda1', u'hypervisor': u'xen', u'client_token': u'', u'virtualization_type': u'hvm', u'architecture': u'x86_64'}) => {
    "msg": "i-04d7c05d0d8af035e"
}
ok: [localhost] => (item={u'network_interfaces': [], u'root_device_type': u'ebs', u'private_dns_name': u'', u'tags': {u'Name': u'crunchify_vms'}, u'key_name': u'crunchifypem', u'image_id': u'ami-92swq93023e0b9cb5', u'cpu_options': {u'threads_per_core': 2, u'core_count': 2}, u'state_transition_reason': u'User initiated (2020-07-11 19:14:20 GMT)', u'hibernation_options': {u'configured': False}, u'capacity_reservation_specification': {u'capacity_reservation_preference': u'open'}, u'public_dns_name': u'', u'placement': {u'availability_zone': u'us-east-2a', u'tenancy': u'default', u'group_name': u''}, u'security_groups': [], u'block_device_mappings': [], u'metadata_options': {u'http_endpoint': u'enabled', u'state': u'pending', u'http_tokens': u'optional', u'http_put_response_hop_limit': 1}, u'state_reason': {u'message': u'Client.UserInitiatedShutdown: User initiated shutdown', u'code': u'Client.UserInitiatedShutdown'}, u'product_codes': [], u'monitoring': {u'state': u'disabled'}, u'ami_launch_index': 0, u'ena_support': True, u'ebs_optimized': False, u'launch_time': u'2020-07-11T19:09:41+00:00', u'instance_id': u'i-0543671e6acaf2326', u'instance_type': u'm5ad.xlarge', u'state': {u'code': 48, u'name': u'terminated'}, u'root_device_name': u'/dev/sda1', u'hypervisor': u'xen', u'client_token': u'', u'virtualization_type': u'hvm', u'architecture': u'x86_64'}) => {
    "msg": "i-0543671e6acaf2326"
}

TASK [terminate] ***********************************************************************************************************************************************
task path: /Users/crunchify/Documents/crunchify_ec2_terminate.yml:14
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: crunchify
<localhost> EXEC /bin/sh -c 'echo ~crunchify && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp `"&& mkdir /Users/crunchify/.ansible/tmp/ansible-tmp-1594496999.16-41259-76591811182344 && echo ansible-tmp-1594496999.16-41259-76591811182344="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1594496999.16-41259-76591811182344 `" ) && sleep 0'
Using module file /Library/Python/2.7/site-packages/ansible/modules/cloud/amazon/ec2.py
<localhost> PUT /Users/crunchify/.ansible/tmp/ansible-local-41195wpzOAR/tmpz2u32e TO /Users/crunchify/.ansible/tmp/ansible-tmp-1594496999.16-41259-76591811182344/AnsiballZ_ec2.py
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1594496999.16-41259-76591811182344/ /Users/crunchify/.ansible/tmp/ansible-tmp-1594496999.16-41259-76591811182344/AnsiballZ_ec2.py && sleep 0'
<localhost> EXEC /bin/sh -c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1594496999.16-41259-76591811182344/AnsiballZ_ec2.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1594496999.16-41259-76591811182344/ > /dev/null 2>&1 && sleep 0'
<localhost> EXEC /bin/sh -c 'echo ~crunchify && sleep 0'
ok: [localhost] => (item={u'network_interfaces': [], u'root_device_type': u'ebs', u'private_dns_name': u'', u'tags': {u'Name': u'crunchify_vms'}, u'key_name': u'crunchifypem', u'image_id': u'ami-92swq93023e0b9cb5', u'cpu_options': {u'threads_per_core': 2, u'core_count': 2}, u'state_transition_reason': u'User initiated (2020-07-11 19:13:48 GMT)', u'hibernation_options': {u'configured': False}, u'capacity_reservation_specification': {u'capacity_reservation_preference': u'open'}, u'public_dns_name': u'', u'placement': {u'availability_zone': u'us-east-2a', u'tenancy': u'default', u'group_name': u''}, u'security_groups': [], u'block_device_mappings': [], u'metadata_options': {u'http_endpoint': u'enabled', u'state': u'pending', u'http_tokens': u'optional', u'http_put_response_hop_limit': 1}, u'state_reason': {u'message': u'Client.UserInitiatedShutdown: User initiated shutdown', u'code': u'Client.UserInitiatedShutdown'}, u'product_codes': [], u'monitoring': {u'state': u'disabled'}, u'ami_launch_index': 1, u'ena_support': True, u'ebs_optimized': False, u'launch_time': u'2020-07-11T19:09:41+00:00', u'instance_id': u'i-04d7c05d0d8af035e', u'instance_type': u'm5ad.4xlarge', u'state': {u'code': 48, u'name': u'terminated'}, u'root_device_name': u'/dev/sda1', u'hypervisor': u'xen', u'client_token': u'', u'virtualization_type': u'hvm', u'architecture': u'x86_64'}) => {
    "ansible_loop_var": "item", 
    "changed": false, 
    "instance_ids": [], 
    "instances": [
        {
            "ami_launch_index": "1", 
            "architecture": "x86_64", 
            "block_device_mapping": {}, 
            "dns_name": "", 
            "ebs_optimized": false, 
            "groups": {}, 
            "hypervisor": "xen", 
            "id": "i-04d7c05d0d8af035e", 
            "image_id": "ami-92swq93023e0b9cb5", 
            "instance_type": "m5ad.4xlarge", 
            "kernel": null, 
            "key_name": "crunchifypem", 
            "launch_time": "2020-07-11T19:09:41.000Z", 
            "placement": "us-east-2a", 
            "private_dns_name": "", 
            "private_ip": null, 
            "public_dns_name": "", 
            "public_ip": null, 
            "ramdisk": null, 
            "region": "us-east-2", 
            "root_device_name": "/dev/sda1", 
            "root_device_type": "ebs", 
            "state": "terminated", 
            "state_code": 48, 
            "tags": {
                "Name": "crunchify_vms"
            }, 
            "tenancy": "default", 
            "virtualization_type": "hvm"
        }, 
        {
            "ami_launch_index": "0", 
            "architecture": "x86_64", 
            "block_device_mapping": {}, 
            "dns_name": "", 
            "ebs_optimized": false, 
            "groups": {}, 
            "hypervisor": "xen", 
            "id": "i-0543671e6acaf2326", 
            "image_id": "ami-92swq93023e0b9cb5", 
            "instance_type": "m5ad.4xlarge", 
            "kernel": null, 
            "key_name": "crunchifypem", 
            "launch_time": "2020-07-11T19:09:41.000Z", 
            "placement": "us-east-2a", 
            "private_dns_name": "", 
            "private_ip": null, 
            "public_dns_name": "", 
            "public_ip": null, 
            "ramdisk": null, 
            "region": "us-east-2", 
            "root_device_name": "/dev/sda1", 
            "root_device_type": "ebs", 
            "state": "terminated", 
            "state_code": 48, 
            "tags": {
                "Name": "crunchify_vms"
            }, 
            "tenancy": "default", 
            "virtualization_type": "hvm"
        }
    ], 
    "invocation": {
        "module_args": {
            "assign_public_ip": null, 
            "aws_access_key": null, 
            "aws_secret_key": null, 
            "count": 1, 
            "count_tag": null, 
            "debug_botocore_endpoint_logs": false, 
            "ebs_optimized": false, 
            "ec2_url": null, 
            "exact_count": null, 
            "group": null, 
            "group_id": null, 
            "id": null, 
            "image": null, 
            "instance_ids": [
                "i-04d7c05d0d8af035e"
            ], 
            "instance_initiated_shutdown_behavior": "stop", 
            "instance_profile_name": null, 
            "instance_tags": null, 
            "instance_type": null, 
            "kernel": null, 
            "key_name": null, 
            "monitoring": false, 
            "network_interfaces": null, 
            "placement_group": null, 
            "private_ip": null, 
            "profile": null, 
            "ramdisk": null, 
            "region": "us-east-2", 
            "security_token": null, 
            "source_dest_check": null, 
            "spot_launch_group": null, 
            "spot_price": null, 
            "spot_type": "one-time", 
            "spot_wait_timeout": 600, 
            "state": "absent", 
            "tenancy": "default", 
            "termination_protection": null, 
            "user_data": null, 
            "validate_certs": true, 
            "volumes": null, 
            "vpc_subnet_id": null, 
            "wait": true, 
            "wait_timeout": 300, 
            "zone": null
        }
    }, 
    "item": {
        "ami_launch_index": 1, 
        "architecture": "x86_64", 
        "block_device_mappings": [], 
        "capacity_reservation_specification": {
            "capacity_reservation_preference": "open"
        }, 
        "client_token": "", 
        "cpu_options": {
            "core_count": 2, 
            "threads_per_core": 2
        }, 
        "ebs_optimized": false, 
        "ena_support": true, 
        "hibernation_options": {
            "configured": false
        }, 
        "hypervisor": "xen", 
        "image_id": "ami-92swq93023e0b9cb5", 
        "instance_id": "i-04d7c05d0d8af035e", 
        "instance_type": "m5ad.4xlarge", 
        "key_name": "crunchifypem", 
        "launch_time": "2020-07-11T19:09:41+00:00", 
        "metadata_options": {
            "http_endpoint": "enabled", 
            "http_put_response_hop_limit": 1, 
            "http_tokens": "optional", 
            "state": "pending"
        }, 
        "monitoring": {
            "state": "disabled"
        }, 
        "network_interfaces": [], 
        "placement": {
            "availability_zone": "us-east-2a", 
            "group_name": "", 
            "tenancy": "default"
        }, 
        "private_dns_name": "", 
        "product_codes": [], 
        "public_dns_name": "", 
        "root_device_name": "/dev/sda1", 
        "root_device_type": "ebs", 
        "security_groups": [], 
        "state": {
            "code": 48, 
            "name": "terminated"
        }, 
        "state_reason": {
            "code": "Client.UserInitiatedShutdown", 
            "message": "Client.UserInitiatedShutdown: User initiated shutdown"
        }, 
        "state_transition_reason": "User initiated (2020-07-11 19:13:48 GMT)", 
        "tags": {
            "Name": "crunchify_vms"
        }, 
        "virtualization_type": "hvm"
    }, 
    "tagged_instances": []
}
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp `"&& mkdir /Users/crunchify/.ansible/tmp/ansible-tmp-1594497000.22-41259-67087912201719 && echo ansible-tmp-1594497000.22-41259-67087912201719="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1594497000.22-41259-67087912201719 `" ) && sleep 0'
Using module file /Library/Python/2.7/site-packages/ansible/modules/cloud/amazon/ec2.py
<localhost> PUT /Users/crunchify/.ansible/tmp/ansible-local-41195wpzOAR/tmpRP7zrl TO /Users/crunchify/.ansible/tmp/ansible-tmp-1594497000.22-41259-67087912201719/AnsiballZ_ec2.py
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1594497000.22-41259-67087912201719/ /Users/crunchify/.ansible/tmp/ansible-tmp-1594497000.22-41259-67087912201719/AnsiballZ_ec2.py && sleep 0'
<localhost> EXEC /bin/sh -c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1594497000.22-41259-67087912201719/AnsiballZ_ec2.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1594497000.22-41259-67087912201719/ > /dev/null 2>&1 && sleep 0'
ok: [localhost] => (item={u'network_interfaces': [], u'root_device_type': u'ebs', u'private_dns_name': u'', u'tags': {u'Name': u'crunchify_vms'}, u'key_name': u'crunchifypem', u'image_id': u'ami-92swq93023e0b9cb5', u'cpu_options': {u'threads_per_core': 2, u'core_count': 2}, u'state_transition_reason': u'User initiated (2020-07-11 19:14:20 GMT)', u'hibernation_options': {u'configured': False}, u'capacity_reservation_specification': {u'capacity_reservation_preference': u'open'}, u'public_dns_name': u'', u'placement': {u'availability_zone': u'us-east-2a', u'tenancy': u'default', u'group_name': u''}, u'security_groups': [], u'block_device_mappings': [], u'metadata_options': {u'http_endpoint': u'enabled', u'state': u'pending', u'http_tokens': u'optional', u'http_put_response_hop_limit': 1}, u'state_reason': {u'message': u'Client.UserInitiatedShutdown: User initiated shutdown', u'code': u'Client.UserInitiatedShutdown'}, u'product_codes': [], u'monitoring': {u'state': u'disabled'}, u'ami_launch_index': 0, u'ena_support': True, u'ebs_optimized': False, u'launch_time': u'2020-07-11T19:09:41+00:00', u'instance_id': u'i-0543671e6acaf2326', u'instance_type': u'm5ad.4xlarge', u'state': {u'code': 48, u'name': u'terminated'}, u'root_device_name': u'/dev/sda1', u'hypervisor': u'xen', u'client_token': u'', u'virtualization_type': u'hvm', u'architecture': u'x86_64'}) => {
    "ansible_loop_var": "item", 
    "changed": false, 
    "instance_ids": [], 
    "instances": [
        {
            "ami_launch_index": "1", 
            "architecture": "x86_64", 
            "block_device_mapping": {}, 
            "dns_name": "", 
            "ebs_optimized": false, 
            "groups": {}, 
            "hypervisor": "xen", 
            "id": "i-04d7c05d0d8af035e", 
            "image_id": "ami-92swq93023e0b9cb5", 
            "instance_type": "m5ad.4xlarge", 
            "kernel": null, 
            "key_name": "crunchifypem", 
            "launch_time": "2020-07-11T19:09:41.000Z", 
            "placement": "us-east-2a", 
            "private_dns_name": "", 
            "private_ip": null, 
            "public_dns_name": "", 
            "public_ip": null, 
            "ramdisk": null, 
            "region": "us-east-2", 
            "root_device_name": "/dev/sda1", 
            "root_device_type": "ebs", 
            "state": "terminated", 
            "state_code": 48, 
            "tags": {
                "Name": "crunchify_vms"
            }, 
            "tenancy": "default", 
            "virtualization_type": "hvm"
        }, 
        {
            "ami_launch_index": "0", 
            "architecture": "x86_64", 
            "block_device_mapping": {}, 
            "dns_name": "", 
            "ebs_optimized": false, 
            "groups": {}, 
            "hypervisor": "xen", 
            "id": "i-0543671e6acaf2326", 
            "image_id": "ami-92swq93023e0b9cb5", 
            "instance_type": "m5ad.4xlarge", 
            "kernel": null, 
            "key_name": "crunchifypem", 
            "launch_time": "2020-07-11T19:09:41.000Z", 
            "placement": "us-east-2a", 
            "private_dns_name": "", 
            "private_ip": null, 
            "public_dns_name": "", 
            "public_ip": null, 
            "ramdisk": null, 
            "region": "us-east-2", 
            "root_device_name": "/dev/sda1", 
            "root_device_type": "ebs", 
            "state": "terminated", 
            "state_code": 48, 
            "tags": {
                "Name": "crunchify_vms"
            }, 
            "tenancy": "default", 
            "virtualization_type": "hvm"
        }
    ], 
    "invocation": {
        "module_args": {
            "assign_public_ip": null, 
            "aws_access_key": null, 
            "aws_secret_key": null, 
            "count": 1, 
            "count_tag": null, 
            "debug_botocore_endpoint_logs": false, 
            "ebs_optimized": false, 
            "ec2_url": null, 
            "exact_count": null, 
            "group": null, 
            "group_id": null, 
            "id": null, 
            "image": null, 
            "instance_ids": [
                "i-0543671e6acaf2326"
            ], 
            "instance_initiated_shutdown_behavior": "stop", 
            "instance_profile_name": null, 
            "instance_tags": null, 
            "instance_type": null, 
            "kernel": null, 
            "key_name": null, 
            "monitoring": false, 
            "network_interfaces": null, 
            "placement_group": null, 
            "private_ip": null, 
            "profile": null, 
            "ramdisk": null, 
            "region": "us-east-2", 
            "security_token": null, 
            "source_dest_check": null, 
            "spot_launch_group": null, 
            "spot_price": null, 
            "spot_type": "one-time", 
            "spot_wait_timeout": 600, 
            "state": "absent", 
            "tenancy": "default", 
            "termination_protection": null, 
            "user_data": null, 
            "validate_certs": true, 
            "volumes": null, 
            "vpc_subnet_id": null, 
            "wait": true, 
            "wait_timeout": 300, 
            "zone": null
        }
    }, 
    "item": {
        "ami_launch_index": 0, 
        "architecture": "x86_64", 
        "block_device_mappings": [], 
        "capacity_reservation_specification": {
            "capacity_reservation_preference": "open"
        }, 
        "client_token": "", 
        "cpu_options": {
            "core_count": 2, 
            "threads_per_core": 2
        }, 
        "ebs_optimized": false, 
        "ena_support": true, 
        "hibernation_options": {
            "configured": false
        }, 
        "hypervisor": "xen", 
        "image_id": "ami-92swq93023e0b9cb5", 
        "instance_id": "i-0543671e6acaf2326", 
        "instance_type": "m5ad.4xlarge", 
        "key_name": "crunchifypem", 
        "launch_time": "2020-07-11T19:09:41+00:00", 
        "metadata_options": {
            "http_endpoint": "enabled", 
            "http_put_response_hop_limit": 1, 
            "http_tokens": "optional", 
            "state": "pending"
        }, 
        "monitoring": {
            "state": "disabled"
        }, 
        "network_interfaces": [], 
        "placement": {
            "availability_zone": "us-east-2a", 
            "group_name": "", 
            "tenancy": "default"
        }, 
        "private_dns_name": "", 
        "product_codes": [], 
        "public_dns_name": "", 
        "root_device_name": "/dev/sda1", 
        "root_device_type": "ebs", 
        "security_groups": [], 
        "state": {
            "code": 48, 
            "name": "terminated"
        }, 
        "state_reason": {
            "code": "Client.UserInitiatedShutdown", 
            "message": "Client.UserInitiatedShutdown: User initiated shutdown"
        }, 
        "state_transition_reason": "User initiated (2020-07-11 19:14:20 GMT)", 
        "tags": {
            "Name": "crunchify_vms"
        }, 
        "virtualization_type": "hvm"
    }, 
    "tagged_instances": []
}
META: ran handlers
META: ran handlers

PLAY RECAP *****************************************************************************************************************************************************
localhost                  : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

bash-3.2$

Open Amazon EC2 Instance page:

Link: https://us-east-2.console.aws.amazon.com/ec2/v2/home?region=us-east-2#Instances:sort=instanceId

Terminate AWS EC2 instances using Ansible - Crunchify Tips

I hope this helps you terminate Amazon EC2 instances via Ansible.

The post Ansible: How to terminate all AWS EC2 instances using Ansible script? appeared first on Crunchify.


Viewing all articles
Browse latest Browse all 1037

Trending Articles