diff --git a/requirements.txt b/requirements.txt index 38f36dac..2dc62b06 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -ansible==2.5.2 +ansible==2.6.11 diff --git a/roles/cloud-ec2/files/stack.yml b/roles/cloud-ec2/files/stack.yml index 3660613b..27a34793 100644 --- a/roles/cloud-ec2/files/stack.yml +++ b/roles/cloud-ec2/files/stack.yml @@ -11,6 +11,12 @@ Parameters: Type: String WireGuardPort: Type: String + UseThisElasticIP: + Type: String + Default: '' +Conditions: + AllocateNewEIP: !Equals [!Ref UseThisElasticIP, ''] + AssociateExistingEIP: !Not [!Equals [!Ref UseThisElasticIP, '']] Resources: VPC: Type: AWS::EC2::VPC @@ -187,6 +193,7 @@ Resources: ElasticIP: Type: AWS::EC2::EIP + Condition: AllocateNewEIP Properties: Domain: vpc InstanceId: !Ref EC2Instance @@ -194,6 +201,14 @@ Resources: - EC2Instance - VPCGatewayAttachment + ElasticIPAssociation: + Type: AWS::EC2::EIPAssociation + Condition: AssociateExistingEIP + Properties: + AllocationId: !Ref UseThisElasticIP + InstanceId: !Ref EC2Instance + + Outputs: ElasticIP: - Value: !Ref ElasticIP + Value: !GetAtt [EC2Instance, PublicIp] diff --git a/roles/cloud-ec2/tasks/cloudformation.yml b/roles/cloud-ec2/tasks/cloudformation.yml index 27977203..a0334ca9 100644 --- a/roles/cloud-ec2/tasks/cloudformation.yml +++ b/roles/cloud-ec2/tasks/cloudformation.yml @@ -12,6 +12,7 @@ PublicSSHKeyParameter: "{{ lookup('file', SSH_keys.public) }}" ImageIdParameter: "{{ ami_image }}" WireGuardPort: "{{ wireguard_port }}" + UseThisElasticIP: "{{ use_existing_eip }}" tags: Environment: Algo register: stack diff --git a/roles/cloud-ec2/tasks/prompts.yml b/roles/cloud-ec2/tasks/prompts.yml index 2993f694..138209f0 100644 --- a/roles/cloud-ec2/tasks/prompts.yml +++ b/roles/cloud-ec2/tasks/prompts.yml @@ -53,3 +53,28 @@ [{{ default_region }}] register: _algo_region when: region is undefined + +- block: + - name: Get existing available Elastic IPs + ec2_eip_facts: + register: raw_eip_addresses + + - set_fact: + available_eip_addresses: "{{ raw_eip_addresses.addresses | selectattr('association_id', 'undefined') | list }}" + + - pause: + prompt: | + Do you want to use a pre-allocated Elastic IP? + (leave blank to allocate a new IP) + {% for eip in available_eip_addresses %} + {{ loop.index }}. {{ eip['public_ip'] }} + {% endfor %} + [''] + register: _use_existing_eip + + - set_fact: + use_existing_eip: >- + {% if _use_existing_eip.user_input is defined and _algo_region.user_input != "" %} + {{ available_eip_addresses[_use_existing_eip.user_input | int -1 ]['allocation_id'] }} + {%- else %}{% endif %} + when: use_existing_eip is undefined \ No newline at end of file