-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmigrate_instance.yml
93 lines (90 loc) · 3.2 KB
/
migrate_instance.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
###################################################
#Author: Almir Candido #
#Contact: https://br.linkedin.com/in/almircandido #
# #
#Ansible Version: 2.9 #
# #
#Requisitos: #
#Install: #
#python-boto #
#python-boto3 #
#pip install boto3 #
###################################################
#Documentation about modules
#https://docs.ansible.com/ansible/latest/modules/ec2_instance_info_module.html
#https://docs.ansible.com/ansible/latest/modules/ec2_ami_module.html
#https://docs.ansible.com/ansible/latest/modules/ec2_ami_copy_module.html
#https://docs.ansible.com/ansible/latest/modules/ec2_module.html
#AWS Create image and copy that image to another region and launch a new instance from this image
---
- hosts: localhost
gather_facts: false
vars:
AWS_ACCESS_KEY: xxxxxxxxxxxxxxxxxxxx #inform access key
AWS_SECRET_KEY: xxxxxxxxxxxxxxxxxxxx #inform secret key
SOURCE_REGION: us-east-1 # N. Virginia
DEST_REGION: us-east-2 # Ohio
TAG_NAME: webserver01
IMAGE_NAME: new_webserver01
INSTANCE_TYPE: t2.micro
INSTANCE_TAG: ohio_webserver01
SECURITY_GROUP_NAME: http
tasks:
- name: Instance facts
ec2_instance_info:
aws_access_key: "{{ AWS_ACCESS_KEY }}"
aws_secret_key: "{{ AWS_SECRET_KEY }}"
region: "{{ SOURCE_REGION }}"
filters:
"tag:Name": "{{ TAG_NAME }}"
register: result
#
- name: Create Image
ec2_ami:
aws_access_key: "{{ AWS_ACCESS_KEY }}"
aws_secret_key: "{{ AWS_SECRET_KEY }}"
region: "{{ SOURCE_REGION }}"
instance_id: "{{ item.instance_id }}"
name: "{{ IMAGE_NAME }}"
wait: yes
tags:
Name: "{{ IMAGE_NAME }}"
loop: "{{ result.instances }}"
register: new_ami
#
- name: Copy ami to another region
ec2_ami_copy:
aws_access_key: "{{ AWS_ACCESS_KEY }}"
aws_secret_key: "{{ AWS_SECRET_KEY }}"
source_region: "{{ SOURCE_REGION }}"
region: "{{ DEST_REGION }}"
source_image_id: "{{ item.image_id }}"
wait: yes
wait_timeout: 1200
register: image_id
loop: "{{ new_ami.results }}"
#
- name: Launch instance in new region
ec2:
aws_access_key: "{{ AWS_ACCESS_KEY }}"
aws_secret_key: "{{ AWS_SECRET_KEY }}"
region: "{{ DEST_REGION }}"
instance_type: "{{ INSTANCE_TYPE }}"
group: "{{ SECURITY_GROUP_NAME }}"
image: "{{ item.image_id }}"
wait: yes
instance_tags:
Name: "{{ INSTANCE_TAG }}"
loop: "{{ image_id.results }}"
- name: Instance facts
ec2_instance_info:
aws_access_key: "{{ AWS_ACCESS_KEY }}"
aws_secret_key: "{{ AWS_SECRET_KEY }}"
region: "{{ DEST_REGION }}"
filters:
"tag:Name": "{{ INSTANCE_TAG }}"
register: new_launch
- name: This is your new instance in {{ DEST_REGION }}
debug:
var: new_launch
...