Ansible: how to stop an EC2 instance launched by another book

If I run an EC2 instance (or set of instances) in Ansible, how can I later reference that instance (or install) and terminate them?

This is in different books. So, in one play I already launched an instance, then later, I want to terminate these instances using another play.

Thanks.

+6
source share
4 answers

You will need to do something like this (working example):

terminate.yml:

- name: terminate single instance hosts: all tasks: - action: ec2_facts - name: terminating single instance local_action: module: ec2 state: 'absent' region: us-east-1 instance_ids: "{{ ansible_ec2_instance_id }}" 

to complete the instance at instance.example.com:

 $ ansible-playbook -i instance.example.com, terminate.yml 

The ec2 fact module will query the metadata service on the instance to get the instance ID. The ec2 module is used to terminate an instance by its identifier.

Note that the ec2_facts module should run on the instances you want to complete, and you probably want to use an inventory file or dynamic inventory to search for an instance by tag instead of accessing them by host name.

+10
source

Well, if we use dynamic inventory, then I recommend using count_tags and exact_count with the ec2 module when creating instances with create. ut

 --- - hosts: localhost connection: local gather_facts: false vars_files: { ./env.yml } tasks: - name: Provision a set of instances ec2: instance_type: "{{ item.value.instance_type }}" image: "{{ image }}" region: "{{ region }}" vpc_subnet_id: "{{ item.value.vpc_subnet_id }}" tenancy: "{{ tenancy }}" group_id: "{{ group_id }}" key_name: "{{ key_name }}" wait: true instance_tags: Name: "{{ env_id }}" Type: "{{ item.key }}" count_tag: Type: "{{ item.key }}" exact_count: "{{ item.value.count }}" with_dict: "{{ servers }}" register: ec2 

The env.yml file contains all of these variables and the server dictionary:

 --- env_id: JaxDemo key_name: JaxMagicKeyPair image: "ami-xxxxxxxx" region: us-east-1 group_id: "sg-xxxxxxxx,sg-yyyyyyyy,sg-zzzzzzzz" tenancy: dedicated servers: app: count: 2 vpc_subnet_id: subnet-xxxxxxxx instance_type: m3.medium httpd: count: 1 vpc_subnet_id: subnet-yyyyyyyy instance_type: m3.medium oracle: count: 1 vpc_subnet_id: subnet-zzzzzzzz instance_type: m4.4xlarge 

Now, if you want to change the number of servers, just change the score in the server dictionary. If you want to delete all of them, we all count to 0.

Or if you want, copy the create.yml file to delete_all.yml and replace

 exact_count: "{{ item.value.count }}" 

with

 exact_count: 0 
+2
source

I will continue jarv's answer with a working example in which you can complete a single or group of servers (s) using an available play.

Suppose you want to terminate an instance (s) that fall under the delete group in the hosts file:

 [delete] XXXX # IP address of an EC2 instance 

Now your game will look like this (in my case, I called it "ec2_terminate.yml"):

 --- - hosts: delete gather_facts: True user: ubuntu sudo: True tasks: # fetch instance data from the metadata servers in ec2 - ec2_facts: # just show the instance-id - debug: msg= "{{ hostvars[inventory_hostname]['ansible_ec2_instance_id'] }}" - hosts: delete gather_facts: True connection: local vars: region: "us-east-1" tasks: - name: destroy all instances ec2: state='absent' region={{ region }} instance_ids={{ item }} wait=true with_items: hostvars[inventory_hostname]['ansible_ec2_instance_id'] 

Now run this play as follows:

 ansible-playbook -i hosts ec2_terminate.yml 
+1
source

When you initially create these instances, make sure that they uniquely identify / label them. Then, on subsequent launches, you can use the dynamic inventory script to populate the list of hosts, as shown here http://docs.ansible.com/intro_dynamic_inventory.html , and then terminate the instances matching your tags.

0
source

Source: https://habr.com/ru/post/979144/


All Articles