-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision_ec2_instance.yml
85 lines (76 loc) · 2.41 KB
/
provision_ec2_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
---
- name: Provision EC2 instance
hosts: localhost
connection: local
gather_facts: false
vars_files:
- vars/config_vars.yml
environment:
AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
pre_tasks:
- name: Update apt repo and cache
apt:
update_cache: true
cache_valid_time: 3600
- name: Install system dependencies
apt:
name:
- python3-pip
state: present
- name: Install python dependencies
pip:
name:
- boto3
state: present
tasks:
- name: Configure EC2 Security Groups
ec2_group:
name: "{{ item.name }}"
description: EC2 security group for RStudio
state: present
rules: "{{ item.rules }}"
region: "{{ aws_region }}"
with_items: "{{ security_groups }}"
tags: "security_group"
- name: Provision EC2 instances
amazon.aws.ec2_instance:
key_name: "{{ item.ssh_key | default('aws_server') }}"
tags:
Name: "{{ item.name | default('') }}"
Application: rstudio_aws
inventory_group: "{{ item.group | default('') }}"
inventory_host: "{{ item.name | default('') }}"
security_groups: "{{ item.security_group | default('') }}"
instance_type: "{{ item.type | default('t2.micro')}}"
image:
id: "{{ aws_ec2_ami }}"
wait: yes
wait_timeout: 50088
network:
assign_public_ip: true
region: "{{ aws_region }}"
volumes:
- device_name: /dev/sda1
ebs:
volume_size: 30
delete_on_termination: true
volume_type: gp2
register: created_instances
with_items: "{{ instances }}"
- name: Add EC2 instances to inventory groups
add_host:
name: "{{ item.1.instances.0.public_ip_address }}"
groups: "{{ item.1.instances.0.tags.inventory_group }},{{ item.1.instances.0.tags.inventory_host }}"
ansible_user: ubuntu
host_key_checking: false
ansible_become_method: sudo
ansible_python_interpreter: /usr/bin/python3
ansible_ssh_private_key_file: ~/.ssh/aws_server.pem
when: item.1.instances is defined
with_indexed_items: "{{ created_instances.results }}"
- hosts: aws
gather_facts: false
tasks:
- name: Wait for hosts to become available
wait_for_connection: