|
202 | 202 | db_instance_identifier: "{{ rds_identifier }}"
|
203 | 203 | register: rds_result
|
204 | 204 |
|
205 |
| - - name: Set 'sshkey_file' variable |
| 205 | + - name: Set variable for SSH private key file path |
206 | 206 | ansible.builtin.set_fact:
|
207 |
| - sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ region | default(aws_region) }} |
| 207 | + deploy_flask_app_bastion_ssh_private_key_path: "~/.{{ resource_prefix }}_id_rsa" |
| 208 | + when: deploy_flask_app_bastion_ssh_private_key_path is undefined |
208 | 209 |
|
209 |
| - - name: Create key pair to connect to the VM |
| 210 | + - name: Create key pair to connect to the workers |
210 | 211 | amazon.aws.ec2_key:
|
211 | 212 | name: "{{ deploy_flask_app_sshkey_pair_name }}"
|
212 |
| - register: rsa_key |
| 213 | + register: keypair_result |
213 | 214 |
|
214 |
| - - name: Save private key into file |
| 215 | + - name: Save private key into a file |
215 | 216 | ansible.builtin.copy:
|
216 |
| - content: "{{ rsa_key.key.private_key }}" |
217 |
| - dest: "{{ sshkey_file }}" |
218 |
| - mode: 0400 |
219 |
| - when: rsa_key is changed |
220 |
| - |
221 |
| - - name: Check if the vm exists |
222 |
| - amazon.aws.ec2_instance_info: |
223 |
| - filters: |
224 |
| - instance-type: "{{ bastion_host_type }}" |
225 |
| - key-name: "{{ deploy_flask_app_sshkey_pair_name }}" |
226 |
| - vpc-id: "{{ vpc.vpc.id }}" |
227 |
| - instance-state-name: running |
228 |
| - register: vm_result |
| 217 | + content: "{{ keypair_result.key.private_key }}" |
| 218 | + dest: "{{ deploy_flask_app_bastion_ssh_private_key_path }}" |
| 219 | + mode: 0600 |
| 220 | + when: keypair_result is changed |
| 221 | + |
| 222 | + - name: Ensure IAM instance role exists |
| 223 | + amazon.aws.iam_role: |
| 224 | + name: "{{ ec2_iam_role_name }}" |
| 225 | + assume_role_policy_document: "{{ lookup('file', 'ec2-trust-policy.json') }}" |
| 226 | + state: present |
| 227 | + create_instance_profile: true |
| 228 | + wait: true |
| 229 | + register: role_output |
229 | 230 |
|
230 | 231 | - name: Create a virtual machine
|
231 |
| - when: vm_result.instances | length == 0 |
232 | 232 | amazon.aws.ec2_instance:
|
233 | 233 | name: "{{ deploy_flask_app_bastion_host_name }}"
|
234 | 234 | instance_type: "{{ bastion_host_type }}"
|
235 | 235 | image_id: "{{ images.images.0.image_id }}"
|
236 | 236 | key_name: "{{ deploy_flask_app_sshkey_pair_name }}"
|
237 | 237 | subnet_id: "{{ subnet.subnet.id }}"
|
| 238 | + ebs_optimized: true |
| 239 | + instance_role: "{{ role_output.iam_role.role_name }}" |
238 | 240 | network:
|
239 | 241 | assign_public_ip: true
|
240 | 242 | groups:
|
241 | 243 | - "{{ secgroup.group_id }}"
|
242 | 244 | security_groups:
|
243 | 245 | - "{{ secgroup.group_id }}"
|
| 246 | + user_data: | |
| 247 | + #!/bin/bash |
| 248 | + yum install -y python3 python-virtualenv sshpass netcat ansible |
244 | 249 | wait: true
|
245 | 250 | state: started
|
246 | 251 | register: vm_result
|
0 commit comments