-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_satellite_vm.yml
136 lines (122 loc) · 4.84 KB
/
create_satellite_vm.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
---
- name: Create a Satellite VM
hosts: localhost
gather_facts: false
vars:
aws_region: "{{ lookup('env', 'AWS_DEFAULT_REGION') }}"
vars_prompt:
- name: aws_keypair_name
prompt: "Enter your AWS keypair name"
private: false
tasks:
- name: Fail if variables not defined
ansible.builtin.assert:
that:
- aws_region is defined
- aws_vpc_subnet_name is defined
- aws_securitygroup_name is defined
- aws_keypair_name is defined
- aws_satellite_instance_ami is defined
- aws_satellite_instance_size is defined
- satellite_vm_name is defined
- satellite_vm_environment is defined
- satellite_vm_type is defined
- purpose is defined
fail_msg: "Required variables not set"
- name: Check if the VM is already provisioned
amazon.aws.ec2_instance_info:
filters:
"tag:Name": "{{ satellite_vm_name }}"
"tag:environment": "{{ satellite_vm_environment }}"
"tag:type": "{{ satellite_vm_type }}"
"tag:purpose": "{{ purpose }}"
instance-state-name:
- pending
- running
- shutting-down
- stopping
- stopped
region: "{{ aws_region }}"
register: existing_vm
- name: Output the IP of existing VM
ansible.builtin.debug:
msg:
- "The Satellite instance looks like already provisioned. Please check the instance."
- "Private IP for the instance: {{ existing_vm.instances[0].private_ip_address }}"
when: existing_vm.instances is defined and existing_vm.instances | length > 0
- name: Create Instance
when: existing_vm.instances | length == 0
block:
- name: Get subnet info
amazon.aws.ec2_vpc_subnet_info:
region: "{{ aws_region }}"
filters:
"tag:Name": "{{ aws_vpc_subnet_name }}"
register: aws_subnet
when: aws_subnet_id is not defined
- name: Save subnet id
ansible.builtin.set_fact:
aws_subnet_id: "{{ aws_subnet.subnets | map(attribute='id') | list | last }}"
when: aws_subnet_id is not defined
- name: Create the instance
amazon.aws.ec2_instance:
network:
assign_public_ip: true
delete_on_termination: true
key_name: "{{ aws_keypair_name }}"
instance_type: "{{ aws_satellite_instance_size }}"
image_id: "{{ aws_satellite_instance_ami }}"
region: "{{ aws_region }}"
security_group: "{{ aws_securitygroup_name }}"
tags:
Name: "{{ satellite_vm_name }}"
environment: "{{ satellite_vm_environment }}"
type: "{{ satellite_vm_type }}"
purpose: "{{ purpose }}"
volumes:
- device_name: /dev/sda1
ebs:
volume_size: 500
delete_on_termination: true
wait: true
vpc_subnet_id: "{{ aws_subnet_id }}"
register: aws_ec2_instance
- name: Output the IP of new VM
ansible.builtin.debug:
msg:
- "Public IP for new instance: {{ aws_ec2_instance.instances[0].public_ip_address | default('') }}"
- "Private IP for new instance: {{ aws_ec2_instance.instances[0].private_ip_address | default('') }}"
when: aws_ec2_instance.instances is defined and aws_ec2_instance.instances | length > 0
- name: Wait for EC2 instance to be ready
ansible.builtin.wait_for:
delay: 60
host: "{{ aws_ec2_instance.instances[0].public_dns_name }}"
port: 22
state: started
timeout: 300
when: aws_ec2_instance.instances is defined and aws_ec2_instance.instances | length > 0
- name: Add the instance to the satellite group
ansible.builtin.add_host:
name: "{{ aws_ec2_instance.instances[0].public_dns_name }}"
groups: satellite
when: aws_ec2_instance.instances is defined and aws_ec2_instance.instances | length > 0
- name: Set public_dns_name
ansible.builtin.set_fact:
public_dns_name: "{{ aws_ec2_instance.instances[0].public_dns_name }}"
when: aws_ec2_instance.instances is defined and aws_ec2_instance.instances | length > 0
- name: Configure satellite server
hosts: satellite
become: true
gather_facts: true
vars:
satellite_public_dns_name: "{{ hostvars.localhost.public_dns_name }}"
vars_prompt:
- name: rhsm_username
prompt: "What is your Red Hat login name?"
private: false
- name: rhsm_passwd
prompt: "What is your Red Hat login password?"
- name: foreman_admin_passwd
prompt: "Enter your Satellite admin password"
roles:
- satellite