Skip to content

Commit e3a6170

Browse files
statikjackivanov
authored andcommitted
AWS support for existing EIP (revised) (#1292)
* Support for associating to existing AWS Elastic IP Signed-off-by: Elliot Murphy <[email protected]> * Backport ec2_eip_facts module for EIP support This means that EIP support no longer requires Ansible 2.6 The local fact module has been named ec2_elasticip_facts to avoid conflict with the ec2_eip_facts module whenever the Ansible 2.6 upgrade takes place. Signed-off-by: Elliot Murphy <[email protected]> * Update from review feedback. Signed-off-by: Elliot Murphy <[email protected]> * Move to the native module. Add additional condition for existing Elastic IP
1 parent 72c8e9e commit e3a6170

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

config.cfg

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,12 @@ cloud_providers:
131131
size: s-1vcpu-1gb
132132
image: "ubuntu-18-04-x64"
133133
ec2:
134-
# Change the encrypted flag to "true" to enable AWS volume encryption, for encryption of data at rest.
135-
# Warning: the Algo script will take approximately 6 minutes longer to complete.
134+
# Change the encrypted flag to "true" to enable AWS volume encryption, for encryption of data at rest.
135+
# Warning: the Algo script will take approximately 6 minutes longer to complete.
136136
encrypted: false
137+
# Set use_existing_eip to "true" if you want to use a pre-allocated Elastic IP
138+
# Additional prompt will be raised to determine which IP to use
139+
use_existing_eip: true
137140
size: t2.micro
138141
image:
139142
name: "ubuntu-bionic-18.04"

roles/cloud-ec2/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ ec2_vpc_nets:
55
cidr_block: 172.16.0.0/16
66
subnet_cidr: 172.16.254.0/23
77
ec2_venv: "{{ playbook_dir }}/configs/.venvs/aws"
8+
existing_eip: ""

roles/cloud-ec2/files/stack.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Parameters:
1111
Type: String
1212
WireGuardPort:
1313
Type: String
14+
UseThisElasticIP:
15+
Type: String
16+
Default: ''
17+
Conditions:
18+
AllocateNewEIP: !Equals [!Ref UseThisElasticIP, '']
19+
AssociateExistingEIP: !Not [!Equals [!Ref UseThisElasticIP, '']]
1420
Resources:
1521
VPC:
1622
Type: AWS::EC2::VPC
@@ -175,13 +181,22 @@ Resources:
175181

176182
ElasticIP:
177183
Type: AWS::EC2::EIP
184+
Condition: AllocateNewEIP
178185
Properties:
179186
Domain: vpc
180187
InstanceId: !Ref EC2Instance
181188
DependsOn:
182189
- EC2Instance
183190
- VPCGatewayAttachment
184191

192+
ElasticIPAssociation:
193+
Type: AWS::EC2::EIPAssociation
194+
Condition: AssociateExistingEIP
195+
Properties:
196+
AllocationId: !Ref UseThisElasticIP
197+
InstanceId: !Ref EC2Instance
198+
199+
185200
Outputs:
186201
ElasticIP:
187-
Value: !Ref ElasticIP
202+
Value: !GetAtt [EC2Instance, PublicIp]

roles/cloud-ec2/tasks/cloudformation.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
PublicSSHKeyParameter: "{{ lookup('file', SSH_keys.public) }}"
1313
ImageIdParameter: "{{ ami_image }}"
1414
WireGuardPort: "{{ wireguard_port }}"
15+
UseThisElasticIP: "{{ existing_eip }}"
1516
tags:
1617
Environment: Algo
1718
register: stack

roles/cloud-ec2/tasks/prompts.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,25 @@
5353
[{{ default_region }}]
5454
register: _algo_region
5555
when: region is undefined
56+
57+
- block:
58+
- name: Get existing available Elastic IPs
59+
ec2_eip_facts:
60+
register: raw_eip_addresses
61+
62+
- set_fact:
63+
available_eip_addresses: "{{ raw_eip_addresses.addresses | selectattr('association_id', 'undefined') | list }}"
64+
65+
- pause:
66+
prompt: >-
67+
What Elastic IP would you like to use?
68+
{% for eip in available_eip_addresses %}
69+
{{ loop.index }}. {{ eip['public_ip'] }}
70+
{% endfor %}
71+
72+
Enter the number of your desired Elastic IP
73+
register: _use_existing_eip
74+
75+
- set_fact:
76+
existing_eip: "{{ available_eip_addresses[_use_existing_eip.user_input | int -1 ]['allocation_id'] }}"
77+
when: cloud_providers.ec2.use_existing_eip

0 commit comments

Comments
 (0)