-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-worker.yml
111 lines (99 loc) · 2.98 KB
/
jenkins-worker.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
- hosts: localhost
tasks:
- name: Find the most recent ubuntu AMI
ec2_ami_find:
name: "ubuntu/images/hvm-ssd/ubuntu-artful-17.10-amd64*"
owner: 099720109477
region: "{{region}}"
architecture: x86_64
virtualization_type: hvm
sort: creationDate
sort_order: descending
sort_end: 1
register: ami_find
- name: Is there a running ec2 instance?
ec2_remote_facts:
region: "{{region}}"
filters:
instance-state-name: running
"tag:Name": "{{ami_tag}}"
register: running_instances
- name: Launch a test instance if we need one.
ec2:
region: "{{region}}"
keypair: ansible
instance_type: "{{instance_type}}"
image: "{{ ami_find.results[0].ami_id }}"
wait: yes
count: 1
instance_tags:
Name: "{{ami_tag}}"
vpc_subnet_id: "{{subnet}}"
assign_public_ip: yes
volumes:
- device_name: /dev/sda1
device_type: gp2
volume_size: 8
delete_on_termination: true
when: running_instances.instances | length == 0
register: created_instances
- name: Build a list of instance id's.
set_fact:
instance_ids: "{{created_instances.instances | default([]) | map(attribute='id') | list }} + {{running_instances.instances | default([]) | map(attribute='id') | list}}"
- name: Build a list of public IP's
set_fact:
ip_addresses: "{{created_instances.instances | default([]) | map(attribute='public_ip') | list }} + {{running_instances.instances | default([]) | map(attribute='public_ip_address') | list}}"
- name: Wait 300 seconds for port 22 to open on our hosts
wait_for:
host: "{{item}}"
port: 22
with_items: "{{ip_addresses}}"
- name: Accept new ssh fingerprints
known_hosts:
name: "{{item}}"
key: "{{ lookup('pipe', 'ssh-keyscan -t ecdsa ' + item) }}"
state: present
with_items: "{{ip_addresses}}"
- name: Add host to in-memory inventory
add_host:
name: "{{ item }}"
groups: jenkins-worker-image
ansible_user: ubuntu
with_items: "{{ip_addresses}}"
- hosts: jenkins-worker-image
gather_facts: no
become: true
roles:
- jenkins-worker-image
- hosts: localhost
tasks:
- name: Build the AMI
ec2_ami:
region: "{{region}}"
instance_id: "{{instance_ids[0]}}"
wait: yes
name: "{{ami_name}}"
tags:
Name: "{{ami_tag}}"
device_mapping:
- device_name: /dev/sda1
volume_type: gp2
size: 8
delete_on_termination: true
- device_name: /dev/sdb
volume_type: gp2
size: 8
delete_on_termination: true
register: new_ami
- debug:
var: new_ami
- name: Clear old ssh fingerprints
known_hosts:
name: "{{item}}"
state: absent
with_items: "{{ip_addresses}}"
- name: Terminate instances that were previously launched
ec2:
region: "{{region}}"
state: 'absent'
instance_ids: "{{instance_ids}}"