-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.yml
More file actions
121 lines (108 loc) · 3.11 KB
/
main.yml
File metadata and controls
121 lines (108 loc) · 3.11 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
- hosts: localhost
become: true
tasks:
# create a security group
- ec2_group:
vpc_id: vpc-03b63bf99652de6a0
region: ap-southeast-1
name: "{{ name }}-jenkins-sg"
description: security group with all traffic allow
rules:
- proto: tcp
ports:
- 8080
cidr_ip: 0.0.0.0/0
rule_desc: allow all on port 8080
register: sg_info
tags:
- run
- debug:
msg: "{{ sg_info.group_id }}"
tags:
- run
# create an instance
- amazon.aws.ec2:
key_name: new-keypair
instance_type: t2.micro
instance_tags:
Name: "{{ name }}-jenkins-server"
image: ami-0ff89c4ce7de192ea
group_id: "{{ sg_info.group_id }}"
region: ap-southeast-1
count_tag:
Name: "{{ name }}-jenkins-server"
exact_count: 1
vpc_subnet_id: subnet-02ce50a9cc336f6ed
assign_public_ip: yes
wait: yes
user_data: |
#!/bin/sh
yum update
wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum upgrade
amazon-linux-extras install java-openjdk11 -y
yum install -y jenkins
systemctl enable jenkins.service
systemctl start jenkins.service
register: ec2_info
tags:
- run
- debug:
msg: "{{ ec2_info.tagged_instances[0].public_ip }}"
tags:
- run
# debugging the ip address of the instance to be deleted
- ec2_instance_info:
region: ap-southeast-1
filters:
"tag:Name": "{{ name }}-jenkins-server"
instance-state-name: [ "shutting-down", "stopping", "stopped", "running" ]
register: ec2_data
tags:
- stop
- debug:
msg: "{{ ec2_data.instances[0].instance_id }}"
- debug:
msg: "{{ ec2_data.instances[0].public_ip_address }}"
tags:
- stop
# delete an instance created
- amazon.aws.ec2:
state: absent
region: ap-southeast-1
instance_ids: "{{ ec2_data.instances[0].instance_id }}"
wait: true
tags:
- stop
# delete a security group
- ec2_group:
state: absent
region: ap-southeast-1
name: "{{ name }}-jenkins-sg"
vpc_id: vpc-03b63bf99652de6a0
region: ap-southeast-1
tags:
- stop
# route53; creating a record set
- community.aws.route53:
state: present
zone: faizanulhaqtest.ml
record: "{{ name }}.faizanulhaqtest.ml"
type: A
value: "{{ ec2_info.tagged_instances[0].public_ip }}"
ttl: 300
tags:
- run
# route53; creating a record set
- community.aws.route53:
state: absent
zone: faizanulhaqtest.ml
record: "{{ name }}.faizanulhaqtest.ml"
type: A
value: "{{ ec2_data.instances[0].public_ip_address }}"
ttl: 300
tags:
- stop